Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 第68天 formData主要是用来做什么的?它的操作方法有哪些? #492

Open
haizhilin2013 opened this issue Jun 22, 2019 · 7 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第68天 formData主要是用来做什么的?它的操作方法有哪些?

@haizhilin2013 haizhilin2013 added the js JavaScript label Jun 22, 2019
@monxiaolee
Copy link

1、将form表单元素的name与value进行组合
2、异步上传表单

@Vi-jay
Copy link

Vi-jay commented Jul 31, 2019

用于后端参数处理为multipart/form-data的情况下 ,现在通常用于上传文件

@censek
Copy link

censek commented Dec 16, 2019

利用 FormData 对象,可以通过 JavaScript 键值对来模拟一系列表单控件,还可以使用 XMLHttpRequest的 send() 方法来异步提交表单。

@Alex-Li2018
Copy link

可以利用formData来上传表单,或者提交上传的文件

document.getElementById('fileBtn').onchange = function () {

        //这是传入的内容是空
        var fm = new FormData();
        //给fm对象添加文件内容
        fm.append('icon',this.files[0]);

        //发送ajax请求
        var xhr = new XMLHttpRequest();
        //设置请求行
        xhr.open('post','getUrl.php');
        //发送数据
        xhr.send(fm);
        //监听事件完成
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4 && xhr.status == 200) {
                
                // console.log(xhr.responseText);
  document.querySelector('.imgShow').style.background = "url("+ xhr.responseText +") no-repeat center/cover";
            }
        }
    }

@MrZ2019
Copy link

MrZ2019 commented Jan 11, 2021

1、将form表单元素的name与value进行组合
2、异步上传表单

@lvpengpeng
Copy link

利用 FormData 对象,可以通过 JavaScript 键值对来模拟一系列表单控件,还可以使用 XMLHttpRequest的 send() 方法来异步提交表单。

@xiaoqiangz
Copy link

可以利用formData来上传表单,或者提交上传的文件

document.getElementById('fileBtn').onchange = function () {

        //这是传入的内容是空
        var fm = new FormData();
        //给fm对象添加文件内容
        fm.append('icon',this.files[0]);

        //发送ajax请求
        var xhr = new XMLHttpRequest();
        //设置请求行
        xhr.open('post','getUrl.php');
        //发送数据
        xhr.send(fm);
        //监听事件完成
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4 && xhr.status == 200) {
                
                // console.log(xhr.responseText);
  document.querySelector('.imgShow').style.background = "url("+ xhr.responseText +") no-repeat center/cover";
            }
        }
    }

基本上是用于form表单合上传文件。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests

8 participants