【发布时间】:2017-06-23 19:10:23
【问题描述】:
这是我在输入更改时将文件上传到服务器的代码
$scope.uploadImage = function (f) {
$http.post('/art',{'image':f},{headers: {'Content-Type': undefined}}).then(function (response) {
console.log(response);
});
}
但是我会在服务器中获得空文件但是我使用正常的文件上传检查我的服务器端代码它可以正常工作,没有任何问题
服务器端
//get empty in angular upload
console.log(req.file('image'));
req.file('image').upload({
// don't allow the total upload size to exceed ~10MB
maxBytes: 10000000
},function whenDone(err, uploadedFiles) {
return res.json({
'status':uploadedFiles
})
});
HTML
<input type="file" ng-model="art.artImage" onchange="angular.element(this).scope().uploadImage(this.files)" name="artImage" id="artImage">
我认为 content-type 在 Angular http post 请求中存在问题
【问题讨论】:
-
调试$scope.uploadImage函数时看到的“f”的值是多少。
-
Json 对象包含像这样的文件信息
1 0 : File lastModified : 1493977671608 lastModifiedDate : Fri May 05 2017 15:17:51 GMT+0530 (IST) __proto__ : Object name : "1493977658097.jpeg" size : 10286 type : "image/jpeg" webkitRelativePath : "" __proto__ : File -
@Jabaa 你能用
f代替{'image':f}吗?这可能会导致内容类型更改为 application/json。 -
@StanislavKvitash 还是同样的错误
-
@Jabaa 你有什么错误?你检查过内容类型吗?在这种情况下是什么?
标签: javascript angularjs node.js sails.js