我使用ng-file-upload将基本的单个文件上传到我的服务器。
我可以选择文件,查看我选择的文件名,并将文件对象($scope.myFiles[0]
)传递给我的角度服务。但是,当用文件填充我的HTTP数据属性时,检查dev工具中的HTTP请求,文件是空对象。
控制器:
UploadService.upload( { data: { file: $scope.myFiles[0], 'category': $scope.selectedCategory}})
.success(function (data) {
////
////
UploadService:
app.factory('UploadService', function ($http, $q) {
return {
upload: function (something) {
console.log(something);
return $http({
method: 'POST',
url: '/upload',
data: something
});
}
}
});
通过上面的内容,检查dev工具的HTTP请求,我看到了:
Request Payload:
{data: {file: {}, category: "Friend Card"}}
data:
{file: {}, category: "Friend Card"}
category:"Friend Card"
file:{}
查看console.log(某事)的输出,我可以通过以下方式看到该文件:
{
$hashKey:"object:107"
lastModified:1465716430000
lastModifiedDate:Sun Jun 12 2016 08:27:10 GMT+0100 (BST)
name:"Postcard.JPG"
size:522622
type:"image/jpeg"
webkitRelativePath:""
}
通过以下方式更改了函数以获得标题类型:
return $http({
method: 'POST',
url: '/upload',
headers: {
'Content-Type': something.data.file.type
},
data: {
"file": something.data.file,
"category": something.data.category
}
});
我可以在开发工具中看到:
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:48
Content-Type:image/jpeg
Cookie:_ga=GA1.1.2023678669.1463291637; _gat=1
Host:localhost:8585
Origin:http://localhost:8585
Referer:http://localhost:8585/pages/
具体来说:Content-Type:image / jpeg所以它看起来是正确的,但文件仍然是空对象。
答案 0 :(得分:0)
答案部分来自上面的danial ......
ng-file-upload lib附带了许多API和方法,可以注入你的角应用程序,其中一个是上传'服务。
我将此注入我的控制器,并在此处跟随jsFiddle:http://jsfiddle.net/danialfarid/maqbzv15/1118/
Upload.upload({
url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
data: {username: $scope.username, file: file},
});