请求时根据控制台来看,contentType是text/plain,而我需要multipart/form-data类型
请问怎么转换呢?
html:
<form ng-submit="processForm()" enctype="multipart/form-data">
<div class="form-group">
<label class="label"><span class="tip" ng-hide="data.id">*</span>ID:</label>
<input class="input" name="id" ng-model="data.id" maxlength="5">
</div>
<div class="form-group">
<input type="file" name="preview" fileread="data.preview"><!--改动在这里-->
</div>
<button type="submit" class="btn btn-default btn-small">保存</button>
</form>
js:
$scope.processForm = function(){
var data = $scope.data,
url = Api.add,
config;
$http({
method: 'POST',
url: url,
data: data,
headers: {'Content-Type': undefined},
}).then(function(result){
.directive("fileread", [function () {
return {
scope: {
fileread: "="
},
link: function (scope, element, attributes) {
element.bind("change", function (changeEvent) {
var reader = new FileReader();
reader.onload = function (loadEvent) {
scope.$apply(function () {
scope.fileread = loadEvent.target.result;
});
}
reader.readAsDataURL(changeEvent.target.files[0]);
});
}
}
}]);
找到解决方法了
http://blog.csdn.net/wei38908...
https://github.com/ghostbar/a...
html
弃用了题目里的directive,改用链接里的angular-file-model
保存的方法:
但是没看懂解决方法里的代码……感觉自己照抄来的也很粗糙……抛砖引玉……
评论里大佬说要用ajax只有通过FormData提交……原来是这个原因啊……头大
https://developer.mozilla.org...