我正在尝试将一个csv文件从JSp上传到控制器。我能够成功上传文件,但是我收到了响应'语法错误:使用angular js '在JSON解析的第1行第1列出现意外字符。我需要从csv文件中读取数据并将其显示在jsp页面中。当我尝试上传时,我遇到了回复问题。
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代码:
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";
}
请让我知道我哪里错了。