spring - Using uiUploader file in AngularJS browser throws 404 bug -
wanted upload file using uiuploader
, have model multipart class: while making post request, browser throws me:
{timestamp: 1492173338591, status: 400, error: "bad request",…} error:"bad request" exception:"org.springframework.web.multipart.support.missingservletrequestpartexception" message:"required request part 'file' not present" path:"/api/file" status:400 timestamp:1492173338591}
when closer, can notice have 'file' property inside html file.
my model class , backend controller looks like:
@data public class multipartmodel { private multipartfile multipartfile; private integer reviewid; } @preauthorize("hasanyauthority('client', 'worker')") @requestmapping(value = "", method = requestmethod.post, headers = {"content-type=multipart/form-data"}, consumes = {"multipart/form-data"}) public void uploadfile(@requestpart("file") list<multipartmodel> files, httpservletrequest httprequest) { principal name = httprequest.getuserprincipal(); if (name.getname() == null) { throw new runtimeexception("brak sesji"); } user userbylogin = userdao.finduserbylogin(name.getname()); file f = null; (multipartmodel file : files) { if (!file.getmultipartfile().isempty()) { review byreviewid = reviewservice.findbyreviewid(file.getreviewid()); try { f = new file(file.getmultipartfile().getoriginalfilename(), file.getmultipartfile().getbytes(), file.getmultipartfile().getname(), file.getmultipartfile().getcontenttype(), new date(), userbylogin, byreviewid); } catch (ioexception e) { e.printstacktrace(); } } if (f != null) { fileservice.uploadfile(f); } } }
and @ frontend i'm using uiuploader
have:
angular.module('sbadminapp').directive('addfile', function () { return { templateurl: 'static/app/scripts/directives/addfile/addfile.html', restrict: 'e', replace: true, controller: function ($scope, $log, uiuploader) { console.log('scope', $scope.reviewid); var multipart = []; $scope.btn_remove = function (file) { $log.info('deleting=' + file); uiuploader.removefile(file); }; $scope.btn_clean = function () { uiuploader.removeall(); }; $scope.btn_upload = function () { $log.info('uploading...'); uiuploader.startupload({ url: '/api/file', concurrency: 2, onprogress: function (file) { $log.info(file.name + '=' + file.humansize); $scope.$apply(); }, oncompleted: function (file, response) { $log.info(file + 'response' + response); } }); }; $scope.files = []; var element = document.getelementbyid('file1'); element.addeventlistener('change', function (e) { var file = e.target.files; console.log('--', e, '===', file); var multipartfile = {multipartfile: file, reviewid: $scope.reviewid}; multipart.push(multipartfile); console.log('mulitpart', multipart); uiuploader.addfiles(multipart); $scope.files = uiuploader.getfiles(); $scope.$apply(); }); $scope.btn_clean(); } } });
and html file:
<div id="uploader"> <div class="row"> <div class="col-md-12"> <h3>dodaj załącznik</h3> <div class="well"> <div> <div style="float:right;"> <button ng-click="btn_upload()">dodaj wszystskie</button> <button ng-click="btn_clean()">usuń wszystkie</button> </div> <input type="file" id="file1" multiple name="file"/> </div> <div ng-repeat="file in files" style="margin-top: 20px;border-bottom-color: antiquewhite;border-bottom-style: double;"> <div><span>{{file.name}}</span> <div style="float:right;"><span>{{file.humansize}}</span> <a ng-click="btn_remove(file)" title="remove list uploaded"><i class="icon-remove">usuń</i></a> </div> </div> <progress style="width:400px;" value="{{file.loaded}}" max="{{file.size}}"></progress> </div> </div> </div> </div> </div>
Comments
Post a Comment