Ajax上传文件
2015-11-08 15:30
411 查看
在使用Ajax提交表单的时候可能会遇到上传文件的情况,可以使用ajaxFileUpload这个JQuery插件来搞定文件的上传。
HTML:
<input type="file" id="file" name="file" class="form-control" onchange="preImg(this.id,'imgPre');"/>
<input type="button" value="确认上传" onclick="ajaxFileUpload()"/>
JS:
/* 图片上传 */
function ajaxFileUpload() {
// 执行上传文件操作的函数
$.ajaxFileUpload({
url : path + '/admin/item/fileUpload',
secureuri : false, // 是否启用安全提交,默认为false
fileElementId : 'file', // 文件选择框的id属性
dataType : 'text', // 服务器返回的格式,可以是json或xml等
success : function(data, status) { // 服务器响应成功时的处理函数
if (data.code == 0) {
$("#fileInfo").html("上传成功.");
} else {
$("#fileInfo").html("上传失败.");
}
},
error : function(data, status, e) {
$("#fileInfo").html("上传失败,请重试.");
}
});
}
后台保存:
@ResponseBody
@RequestMapping(value = "/item/fileUpload")
public String addUser(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request,
HttpServletResponse response) throws IOException {
String message = null;
if (file.isEmpty()) {
} else {
String format = DateUtil.format(DateUtil.getNow(), "yyyyMMdd");
StringBuffer sb = new StringBuffer().append(File.separatorChar).append("fileUpload")
.append(File.separatorChar).append("images").append(File.separatorChar).append("item")
.append(File.separatorChar).append(format);
String path = sb.toString();
String realPath = request.getSession().getServletContext().getRealPath(path);
long random = RandomUtil.getRandom(100000, 999999);
String oriName = file.getOriginalFilename();
String suffix = oriName.substring(oriName.lastIndexOf("."));
String filename = DateUtil.getTime() + "" + random + suffix;
try {
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, filename));
message = "0`" + path + File.separatorChar + filename;
} catch (IOException e) {
}
}
return message;
}
HTML:
<input type="file" id="file" name="file" class="form-control" onchange="preImg(this.id,'imgPre');"/>
<input type="button" value="确认上传" onclick="ajaxFileUpload()"/>
JS:
/* 图片上传 */
function ajaxFileUpload() {
// 执行上传文件操作的函数
$.ajaxFileUpload({
url : path + '/admin/item/fileUpload',
secureuri : false, // 是否启用安全提交,默认为false
fileElementId : 'file', // 文件选择框的id属性
dataType : 'text', // 服务器返回的格式,可以是json或xml等
success : function(data, status) { // 服务器响应成功时的处理函数
if (data.code == 0) {
$("#fileInfo").html("上传成功.");
} else {
$("#fileInfo").html("上传失败.");
}
},
error : function(data, status, e) {
$("#fileInfo").html("上传失败,请重试.");
}
});
}
后台保存:
@ResponseBody
@RequestMapping(value = "/item/fileUpload")
public String addUser(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request,
HttpServletResponse response) throws IOException {
String message = null;
if (file.isEmpty()) {
} else {
String format = DateUtil.format(DateUtil.getNow(), "yyyyMMdd");
StringBuffer sb = new StringBuffer().append(File.separatorChar).append("fileUpload")
.append(File.separatorChar).append("images").append(File.separatorChar).append("item")
.append(File.separatorChar).append(format);
String path = sb.toString();
String realPath = request.getSession().getServletContext().getRealPath(path);
long random = RandomUtil.getRandom(100000, 999999);
String oriName = file.getOriginalFilename();
String suffix = oriName.substring(oriName.lastIndexOf("."));
String filename = DateUtil.getTime() + "" + random + suffix;
try {
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, filename));
message = "0`" + path + File.separatorChar + filename;
} catch (IOException e) {
}
}
return message;
}
相关文章推荐
- poj 2074 Line of Sight (计算几何,细节题)
- Study of the Symbolic Exection
- Linux下安装nexus
- 20135321余佳源——信息安全系统设计基础第九周学习总结
- sed命令的用法
- 九度oj 和为S的连续正数序列 1354 (机智DP) 好题
- Linux远程图形界面
- 2015上海马拉松线上跑感悟-人生如同一场马拉松
- 典型用户和场景
- Linux Terminator
- [BeiJing2006]狼抓兔子 (s-t平面图求最大流,堆优dijkstra)
- Spring destory-method
- Android使用限定符
- 读《大道至简——从编程到工程》有感
- [NOIP2015总结]下雪的比赛
- win7 64位系统环境CW_ColdFire_7.2_SE安装教程
- MTP in Android
- Linux内存管理
- hdu 1528 Card Game Cheater(最小覆盖)
- 最长公共子序列