i trying upload csv file jsp controller. can able upload file sucessfully getting response 'syntax error:unexpected character @ line 1 column 1 of json parse using angular js'. need read data csv file , display in jsp page. when trying upload getting issue response.
jsp code:
<form action="robiibiaascsvupload/fileuploadinfo" id="fileuploadform" method="post" enctype="multipart/form-data" data-ng-controller="fileuploadcontroller"> <table width="100%" border="0" cellspacing="5" cellpadding="0" id="uploadfile"> <tr> <td align="left" valign="middle">upload file:</td><br/> <td align="left" valign="middle"> <input name="file2" id="file2" type="file" file-model="file2"/><br/> </td> <td><input type="button" value="submit" ng-click="uploadformdata();"></td> </tr> <tr> <td align="left" valign="middle"> </td> </tr> </table> </form>
javascript code:
var app = angular.module('formsubmit', []); app.controller('fileuploadcontroller',function($scope,$http){ $scope.uploadformdata=function() { // headers: {'content-type': false}, // file2.files[0] //alert(file2.files[0].name); var omyform = new formdata(); omyform.append("file",file2.files[0]); $http({ method: 'post', url: 'uploadfile', headers: {'content-type': undefined}, data: omyform, transformrequest: function(response, headersgetterfunction) { headers = {'content-type': 'application/json'}; //alert(response); //return response; return response; },headers: { 'content-type': undefined } }).success(function(data, headers) { alert(data); }) .error(function(data, headers) { alert("exception"+data); }); } });
spring mvc controller :
@requestmapping(value = "/uploadfile") public @responsebody string uploadfile(httpservletrequest request, @requestparam(value = "file") multipartfile file) { stringbuilder result=null; modelmap map=new modelmap(); try { system.out.println(file.getoriginalfilename()); system.out.println(file.getbytes()); inputstream in=file.getinputstream(); bufferedreader reader = new bufferedreader(new inputstreamreader(in)); result = new stringbuilder(); string line; while((line = reader.readline()) != null) { result.append(line); result.append('\n'); } map.addattribute("status", "success"); map.addattribute("list", result.tostring()); system.out.println(result.tostring()); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return "success"; }
please let me know went wrong.
Comments
Post a Comment