栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring Boot文件上传错误请求400

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot文件上传错误请求400

我找到了错误的原因。它不是来自我的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--


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/405475.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号