[英]unsupported Media type 415 for file upload with Spring mvc restcontroller and angularjs
When i am trying to hit the angularjs $http.post which contains file upload details its giving me unsupported media type. 当我尝试点击angularjs $ http.post,其中包含文件上传详细信息时,它给了我不受支持的媒体类型。 I am using backend spring mvc rest controller.
我正在使用后端spring mvc rest控制器。 The following is the code snippet.
以下是代码段。
<ul><li><h4>Document Type</h4> <select name="docType" ng-model="docType" name="docType"> <option value="Report1">Report1</option> <option value="Report2">Report2</option> </select> <input type="file" ng-model-instant id="fileToUpload" onchange="angular.element(this).scope().setFiles(this)" /> </li> <li><h4>Comments:</h4><textarea></textarea></li> <li><button class="btn-panel-blue" type="submit">Saves</button></li> <li><a href="#/searchProjects">Cancel</a></li> </ul>
angular.module('app') .controller('prjCntrl', function ($scope,projectService, sharedService, $location, $log, $http, $state) { $scope.setFiles = function(element) { $scope.$apply(function(scope) { $scope.uploadedFile ; for (var i = 0; i < element.files.length; i++) { $scope.uploadedFile=element.files[i]; break; } }); }; $scope.updateProject = function updateProject() { var formData=new FormData(); formData.append('docType',angular.toJson($scope.projectData.docType,true)); formData.append("uploadFile",$scope.uploadedFile); var url=baseURL + '/updateProject.do'; $http.post(url, formData, { transformRequest: angular.identity, headers: {'Content-Type': undefined} }) .success(function(){alert("success");}) .error(function(){ }); }; } );
**General:**
Request URL:http://localhost:9090/SpringMVCAngularJS/updateProject.do
Request Method:POST
Status Code:415 Unsupported Media Type
Remote Address:[::1]:9090
***Response Headers:***
view source
Content-Language:en
Content-Length:1048
Content-Type:text/html;charset=utf-8
Date:Wed, 03 Aug 2016 15:06:05 GMT
Server:Apache-Coyote/1.1
***Request Headers:***
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Authorization:Basic YmVlcDpib29w
Connection:keep-alive
Content-Length:1011
Content-Type:multipart/form-data; boundary=---- WebKitFormBoundaryagdf0LOX4AuXY6SI
Cookie:JSESSIONID=96052E348C6E52F53A594A179E65DE6D
Host:localhost:9090
Origin:http://localhost:9090
Referer:http://localhost:9090/SpringMVCAngularJS/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
here is the header information from chrome headers/request and response data 这是chrome标头/请求和响应数据中的标头信息
**General:** Request URL:http://localhost:9090/SpringMVCAngularJS/updateProject.do Request Method:POST Status Code:415 Unsupported Media Type Remote Address:[::1]:9090 ***Response Headers:*** view source Content-Language:en Content-Length:1048 Content-Type:text/html;charset=utf-8 Date:Wed, 03 Aug 2016 15:06:05 GMT Server:Apache-Coyote/1.1 ***Request Headers:*** view source Accept:application/json, text/plain, */* Accept-Encoding:gzip, deflate Accept-Language:en-US,en;q=0.8 Authorization:Basic YmVlcDpib29w Connection:keep-alive Content-Length:1011 Content-Type:multipart/form-data; boundary=---- WebKitFormBoundaryagdf0LOX4AuXY6SI Cookie:JSESSIONID=96052E348C6E52F53A594A179E65DE6D Host:localhost:9090 Origin:http://localhost:9090 Referer:http://localhost:9090/SpringMVCAngularJS/index.html User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
Please help me with the solution. 请帮我解决。
try to send the comment or other data in request header and send file to contoller ,in controller use" @Requestpart" for accepting file. 尝试在请求标头中发送注释或其他数据,并将文件发送到控制器,在控制器中使用“ @Requestpart”来接受文件。 Use below code for reference..
使用下面的代码作为参考。
@Produces("text/plain;charset=UTF-8")
@RequestMapping(value = "/fileUpload/", method = RequestMethod.POST)
public @ResponseBody ResponseCodeModel fileUpload(@RequestPart MultipartFile file,
@RequestHeader(value = "comment") String comment,
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.