js 使用ajaxFileUpload上传文件:
ajaxFileUpload: function (s) {
}
返回的数据并不是json格式,而是:
{"JsonKey":"JsonValue"}
从ajax源码可以知道,json数据是通过eval生成的:
if ( type == "json")
eval( "data = " + data);
所以解决方式:
- 在前台将
{"JsonKey":"JsonValue"}
过滤,这样就是干净的json数据 - 查找eval解析失败的原因
- 也是我最终的解决办法。。
不再接收json格式,而是采用默认的 text/html格式
对接收的返回数据做处理:
var str = $(data).find("body").text();//获取返回的字符串
var json = $.parseJSON(str);//把字符串转化为json对象
中文乱码问题
因为格式没有统一成utf-8,可以在js里查看json的格式,java后台可以设置:
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
附:SpringBoot上传附件默认 multi 1M 单请求文件10M,可以单独设置
Spring Boot embraces the Servlet 3 javax.servlet.http.Part API to support uploading files. By default Spring Boot configures Spring MVC with a maximum file of 1MB per file and a maximum of 10MB of file data in a single request. You may override these values, as well as the location to which intermediate data is stored (e.g., to the /tmp directory) and the threshold past which data is flushed to disk by using the properties exposed in the MultipartProperties class. If you want to specify that files be unlimited, for example, set the spring.http.multipart.max-file-size property to -1.
设置如下:
/**
- 文件上传配置
- @return
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//单个文件最大
factory.setMaxFileSize("10240KB"); //KB,MB
/// 设置总上传数据总大小
factory.setMaxRequestSize("102400KB");
return factory.createMultipartConfig();
}