Ich habe ein Formular mit zwei input text
und einem upload
. Ich muss es an den Server senden, aber ich habe ein Problem beim Verketten der Datei mit dem Text. Der Server erwartet diese Antwort:Senden Sie FormData mit anderen Feld in Angular
"title=first_input" "text=second_input" "file=my_file.pdf"
Dies ist html:
<input type="text" ng-model="title">
<input type="text" ng-model="text">
<input type="file" file-model="myFile"/>
<button ng-click="send()">
Dies ist der -Controller:
$scope.title = null;
$scope.text = null;
$scope.send = function(){
var file = $scope.myFile;
var uploadUrl = 'my_url';
blockUI.start();
Add.uploadFileToUrl(file, $scope.newPost.title, $scope.newPost.text, uploadUrl);
};
Dies ist die Richtlinie fileModel:
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(){
scope.$apply(function(){
modelSetter(scope, element[0].files[0]);
});
});
}
};
Und dies ist der Dienst, die den Server aufrufen:
this.uploadFileToUrl = function(file, title, text, uploadUrl){
var fd = new FormData();
fd.append('file', file);
var obj = {
title: title,
text: text,
file: fd
};
var newObj = JSON.stringify(obj);
$http.post(uploadUrl, newObj, {
transformRequest: angular.identity,
headers: {'Content-Type': 'multipart/form-data'}
})
.success(function(){
blockUI.stop();
})
.error(function(error){
toaster.pop('error', 'Errore', error);
});
}
Wenn ich zu senden versuchen, erhalte ich Fehler 400, und die Antwort ist: Multipart form parse error - Invalid boundary in multipart: None
. Die Nutzlast der Anforderung ist: {"title":"sadf","text":"sdfsadf","file":{}}
Gibt es einen Grund, warum Sie' Serialisierung der 'FormData'? – Kyle