简体   繁体   English

如何使用AJAX上传文件

[英]How do I use AJAX to upload file

I want to use AJAX to upload. 我想使用AJAX上传。

This is my HTML code 这是我的HTML代码

<form id="upload-form" enctype="multipart/form-data" method="post" action="upload">
    File Name:<input type="text" name="name" /><br />
    <input type="file" name="file" /><br />
    <input type="submit" />
</form>

This is my Javascript code: 这是我的Javascript代码:

var file = document.querySelector("[type=file]").files[0], 
    reader = new FileReader; 

reader.onload = function(e){
    var xhr = new XMLHttpRequest
    xhr.open("POST", document.querySelector("form#upload-form").action, true);

    //set payload
    var boundary = '------multipartformboundary' + (new Date).getTime().toString(16);
    var dashdash = '--';
    var crlf = '\r\n';

    var payload = '';
    payload += boundary;
    payload += crlf;

    payload += 'Content-Disposition: form-data; name="file"';
    payload += crlf;    

    payload += 'Content-Type: ' + file.type;
    if (file.fileName) {
        payload += '; filename="' + file.name + '"';
    }
    payload += crlf + crlf;

    payload += reader.result;
    payload += crlf;


    payload += boundary;
    payload += crlf;
    payload += 'Content-Disposition: form-data; name="name"';
    payload += crlf + crlf;
    payload += document.querySelector("[name=name]").value || ""
    payload += crlf;

    payload += boundary;
    payload += dashdash;

    xhr.setRequestHeader('Content-type', 'multipart/form-data; boundary=' + boundary)
    xhr.send(payload)
}
reader.readAsBinaryString(file)

The payload of the request looks like: 请求的有效负载如下:

------multipartformboundary14e6631b3eb
Content-Disposition: form-data; name="file"
Content-Type: image/png

PNG


(some binary code)
------multipartformboundary14e6631b3eb
Content-Disposition: form-data; name="name"

My test file
------multipartformboundary14e6631b3eb--

However, the request status is 400 . 但是,请求状态为400

The description says "request sent by the client was syntactically incorrect." 描述说“客户端发送的请求在语法上是不正确的”。 (Server is Servlet + Tomcat 8) (服务器是Servlet + Tomcat 8)

When I just submit the form without AJAX, it can upload successfully. 当我只提交不带AJAX的表单时,它可以成功上传。 Therefore, I do not think it is the server's issue. 因此,我认为这不是服务器的问题。

In the content-type line: 在内容类型行中:

xhr.setRequestHeader('Content-type', 'multipart/form-data; boundary=' + boundary)

Your code provide: 您的代码提供:

multipart/form-data; boundary=------xxxxxxxxx

But, it should be: 但是,应该是:

multipart/form-data; boundary=----xxxxxxxxx

Notice the number of dash. 注意破折号。

So change it to: 因此将其更改为:

xhr.setRequestHeader('Content-type', 'multipart/form-data; boundary=' + boundary.substr(2))

And try again. 然后再试一次。

                var Self = this;
                    Self.xhr = new XMLHttpRequest();
                    Self.url = "./connect.php"; // your file to post data
                    Self.xhr.open("POST",Self.url,true);
                    Self.xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    Self.xhr.onreadystatechange = function() {
                    if(Self.xhr.readyState == 4 && Self.xhr.status == 200) {
                    Self.return_data = Self.xhr.responseText.trim();

                    }
              Self.xhr.send(Self.logData);

This is how i POST data to my server using XHR request. 这就是我使用XHR请求将数据发布到服务器的方式。 and connect.php will update my Database. 和connect.php将更新我的数据库。 so you can add what ever the data you need to send to "Self.logData" before making XHR request. 因此,您可以在发出XHR请求之前将需要发送的所有数据添加到“ Self.logData”。 and you need a file like connect.php or some other to handle the post data at server side . 并且您需要一个类似于connect.php的文件或其他文件来处理服务器端的发布数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM