[英]File upload in Angular JS
我正在嘗試通過angularjs上傳AJAX文件。 它告訴我表單數據的格式不正確或類似的格式。
這是我的html:
<TR>
<TD class="labelWide" nowrap="nowrap">Upload PDF</TD>
<TD class="required"> </TD>
<TD class="data">
<input type="file" npr-uploader="temporary_upload">
<button ng-click="uploadFile('pdf')">Load File</button><BR>
<span ng-repeat="fileItem in data.pdfFiles">{{fileItem.filename}}<BR></span>
</TD>
</TR>
這是我的指令:
nprDirectives.directive('nprUploader', ['$parse', function($parse){
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.nprUploader);
var modelSetter = model.assign;
element.bind('change', function(){
scope.$apply(function(){
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
這就是所謂的uploadFile
函數:
$scope.uploadFile = function(arg_type){
$scope.data.uploadFile($scope.temporary_upload, arg_type);
};
這是我的數據服務中用於上傳的功能。
$scope.uploadFile = function(arg_file,arg_type,arg_key){
var fd = new FormData();
fd.append("attachment", arg_file);
var urlType = "other";
if(arg_type == "pdf"){
urlType = "pdf";
}
$http.post( contextPath+'/uploadFile/ajax/upload/'+urlType, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(stuff happens)
.error(stuff happens);
最后,這是我的錯誤信息:
HTTP Status 400 - Please check your Data
type: Status Report
message: Please check your Data
description: The request sent by the client was syntactically incorrect (Please check your Data.).
我還繼續查看控制台中的ajax帖子,並看到了以下內容:
-----------------------------168221372516176
Content-Disposition: form-data; name="attachment"; filename="busicard.pdf"
Content-Type: application/pdf
%PDF-1.4
(etc lots of gobblygook because PDF with image)
由於我發現我以正確的方式格式化AJAX調用,因此此操作已關閉。 問題實際上出在接收文件的Java中。 在不對JavaScript進行更改的情況下,我設法獲得了要上傳的文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.