問題:解決上傳文件IE瀏覽器彈出下載框bug?

控制器方法的返回值必須以String返回,再由js處理轉換成json對象
 
$.ajaxFileUpload({
url: "/project/proj_conver_upload", // 須要連接到服務器地址
secureuri: false,
fileElementId: "fileBtn", // 文件選擇框的id屬性
data: {},
dataType: 'json', // 服務器返回的格式,能夠是json
complete: function(response, status) {
var responseText = response.responseText;
if (responseText.indexOf(">") != -1&& (responseText.indexOf("PRE") != -1 || responseText.indexOf("pre") != -1)) {
responseText = responseText.substring(responseText.indexOf(">") + 1, responseText.lastIndexOf("<"));
}
var data = eval("(" + responseText + ")");//轉換成json對象
 
if(data.retCode!="200"){
showWarning(data.msg);
return;
}else{
$("#avaImg").attr("src", data.results);
$("#projFileName").val(fileName);
$("#ava").val(data.results);
$("#fileBtn").css("display", "block");
$("#projFileNameSpan").html("");
}
},
error: function(data, status, e) {
console.log(data.msg);
}
});
 
 
 
服務器中控制層:
@RequestMapping(value = "/proj_conver_upload",method = RequestMethod.POST,produces = { "text/html;charset=UTF-8" })
@ResponseBody
public String projConverUpload(@RequestParam(value = "fileBtn", required = true) MultipartFile file,
HttpServletRequest request,HttpServletResponse response) throws Exception {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
long size = file.getSize()/1014/1014;
LOG.debug("logoImgSize: "+ size+"M");
if(size>=2){
return JsonHelper.toJsonString(new Result(CodeConstant.RETCODE_500, "圖片附件不能大於2M"));
}
 
String fileName = file.getOriginalFilename();
if(!FileHelper.isPic(fileName)){
return JsonHelper.toJsonString(new Result(CodeConstant.RETCODE_500, "請上傳正確的圖片格式"));
}
fileName = UUIDFileName(file.getOriginalFilename());
FileUploadUtil.transferTo(file, getTempDic(request), fileName);
Result<String> result = new Result<String>();
result.setResults(FileConstant.TEMP_FILE_ROOT+"/"+fileName);
 
return JsonHelper.toJsonString(result);
}
轉載自同事工做筆記
相關文章
相關標籤/搜索