我正在尝试将文件以及表单数据从Angularjs客户端上传到web api。我在网上找到了一些参考代码并以同样的方式实现。 Web API方法中的第一行代码是if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
返回false并将失败返回给客户端。我不知道我做错了什么......
以下是我Angularjs中的代码
var _uploadNotes = function (notes, files) {
var deferred = $q.defer();
$http({
method: 'POST',
url: "api/issue/uploadNotes",
headers: { 'Content-Type': false },
transformRequest: function (data) {
var formData = new FormData();
formData.append("model", angular.toJson(data.model));
for (var i = 0; i < data.files.length; i++) {
formData.append("file" + i, data.files[i]);
}
return formData;
},
data: { model: notes, files: files }
}).
success(function (data, status, headers, config) {
deferred.resolve();
}).
error(function (data, status, headers, config) {
deferred.reject();
});
return deferred.promise;
};
答案 0 :(得分:4)
简单地更换
headers: { 'Content-Type': false},
与headers: { 'Content-Type': undefined},
整个代码:
var _uploadNotes = function (notes, files) {
var deferred = $q.defer();
$http({
method: 'POST',
url: "api/issue/uploadNotes",
headers: { 'Content-Type': undefined},
transformRequest: function (data) {
var formData = new FormData();
formData.append("model", angular.toJson(data.model));
for (var i = 0; i < data.files.length; i++) {
formData.append("file" + i, data.files[i]);
}
return formData;
},
data: { model: notes, files: files }
}).
success(function (data, status, headers, config) {
deferred.resolve();
}).
error(function (data, status, headers, config) {
deferred.reject();
});
return deferred.promise;
};