我想用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。谢谢!
答案 0 :(得分:0)
使用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);
};
}]);