简体   繁体   English

Javascript XMLHttpRequest FormData发送不起作用

[英]Javascript XMLHttpRequest FormData sending doesn't work

I want to upload some files to my server with a drag'n'drop angularjs directive. 我想使用drag'n'drop angularjs指令将一些文件上传到服务器。 I've hardcoded for hours but I haven't still found the error. 我已经硬编码了几个小时,但仍然没有发现错误。 I have that angularjs code: 我有那个angularjs代码:

var fd = new FormData();

for(x = 0; x < files.length; x++)
    fd.append("file[]", files[x]);
    fd.append('type', 'upload');
    xhr = new XMLHttpRequest();
xhr.open("post", "/../RESOURCES/API/explorer.php", true);
    xhr.upload.addEventListener("progress", function(e) {
    var pc = parseInt(100 - (e.loaded / e.total * 100));
    console.log(pc);
}, false);
    xhr.onload = function() {
    console.log(xhr.responseText);
};
xhr.send(fd);

But my APIs returns empty $_POST and $_FILES. 但是我的API返回空的$ _POST和$ _FILES。 Thanks! 谢谢!

With Angular JS, I've achieved file upload with the following code: 使用Angular JS,我可以使用以下代码实现文件上传:

angular.module('frApp')
  .service('fileUpload', ['$http',function ($http) {

    this.uploadFileToUrl = function(file, uploadUrl, success, error){
      var fd = new FormData();
      fd.append('file', file);
      $http.post(uploadUrl, fd, {
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
      }).success(success)
        .error(error);
    };
  }]);

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

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