![](/img/trans.png)
[英]Javascript XMLHttpRequest with formdata sending empty body
[英]Javascript XMLHttpRequest FormData sending doesn't work
我想使用drag'n'drop angularjs指令將一些文件上傳到服務器。 我已經硬編碼了幾個小時,但仍然沒有發現錯誤。 我有那個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);
但是我的API返回空的$ _POST和$ _FILES。 謝謝!
使用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.