我正在处理Spring MVC中的csv文件(有大量记录)处理。 如果文件有错误的记录,我需要写一个文件中有错误的所有记录,我还需要在网页上显示错误信息。
我的问题是,当我将记录写入文件时,应该如何显示错误消息。
以下是我的控制器代码无效
@ResponseBody
@RequestMapping(method = RequestMethod.POST, params = "bulkupload", produces = MediaType.APPLICATION_JSON_VALUE)
public JsonResponse handleFileUpload(@RequestParam(value = "file") MultipartFile csvFile, HttpServletResponse response) throws IOException{
JsonResponse jsonResponse = new JsonResponse();
try{
RESULT result = bulkUploadService.processBulkData(csvFile.getInputStream());
if(result.code==RESULT.DATA_ERROR){
jsonResponse.setResult( DATA_ERROR);
response.setContentType("text/csv");
response.setHeader("Content-disposition", "attachment;filename=errorList.csv");
writeErrorToCSV(result.errorResult, new OutputStreamWriter(response.getOutputStream()));
}else if (result.code == RESULT.SUCCESS){
jsonResponse.setResult(SUCCESS);
}
jsonResponse.setStatus(JSON_RESPONSE_SUCCESS);
}
catch(Exception e){
LOGGER.error(ExceptionUtils.getStackTrace(e));
jsonResponse.setStatus(JSON_RESPONSE_ERROR);
jsonResponse.setResult(UNKOWN_ERROR);
}
return jsonResponse;
}
客户端代码:
$("#bulkupload_form").submit(function(e){
e.preventDefault();
var mUrl= $(this).attr("action");
var oMyForm = new FormData();
oMyForm.append("file", $("#csvFile")[0].files[0]);
$.ajax({
url : mUrl,
type: "post",
data: oMyForm,
enctype: 'multipart/form-data',
processData: false,
contentType:false,
success : function(response) {
if(response.status == "SUCCESS"){
alert(response.result);
}
} ,
error: function() {
}
});
});