我找到了错误的原因。它不是来自我的Spring控制器,而是来自我的angularJS html代码。
这是我的上载输入字段:
<div ng-> <label name=file-field-label>Volume Dataset</label> <input value=file name=file ng-model=upload.file required id=file file_ext_validation type="file" extensions="nii,NII,gz,jpeg,JPG"/> <divng-show="volume_upload_form.volume.$invalid"> <small ng-show="volume_upload_form.volume.$error.fileExtValidation"> File must be of type ".nii" </small> </div> </div>
如您所见,也是使用默认的ng-model与我的角度控制器通信(“ upload”是我的Controller别名)。
ng-model=upload.file
但是Angular默认不支持文件输入html字段。因此,仅包含文件路径的字符串存储在upload.file中,而不是实际的文件对象。我必须编写一个自定义指令:
var fileModel = function ($parse) { 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]); }); }); } };}module.exports = fileModel;返回实际的File对象。我新上传的html代码如下:
<div ng-> <label name=file-field-label>Volume Dataset</label> <input name=file file-model="upload.fileModel" ng-model="file" required file_ext_validation type="file" extensions="nii,NII,gz,jpeg,JPG"/> <divng-show="volume_upload_form.volume.$invalid"> <small ng-show="volume_upload_form.volume.$error.fileExtValidation"> File must be of type ".nii" </small> </div> </div>
你可以看到
file-model="upload.fileModel"
将文件对象从文件输入字段链接到角度控制器。这是正确的请求有效负载。
------WebKitFormBoundaryR1AXAwLepSUKJB3iContent-Disposition: form-data; name="file"; filename="test.nii.gz"Content-Type: application/x-gzip------WebKitFormBoundaryR1AXAwLepSUKJB3i--



