jQuery实现异步上传⼀个或多个⽂件本⽂实例为⼤家分享了jQuery实现异步上传⼀个或多个⽂件的具体代码,供⼤家参考,具体内容如下⾸先使⽤SpringMvc⽂件上传,需要引⼊第三⽅上传⽂件的jar:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
响应json需要导⼊的包:
<dependency>
<groupId>com.</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.</groupId>万能断路器为什么要储能
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
接下来看jsp⽂件:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>⾸页</title>
</head>
<body>
<p>同步上传⼀个⽂件</p>
<form action="upload/testUpload" method="post" enctype="multipart/form-data">
<input type="file" name="upload"><br>
<input type="submit" value="上传">
</form>
<hr>
<p>异步上传⼀个⽂件</p>
<form id="formData" method="post" enctype="multipart/form-data">
<input type="file" name="upload"><br>
<input id="sub" type="button" value="上传">
</form>
<hr>
机器人编程培训费用<p>异步上传⼀个⽂件,且表单有其他数据</p>ssm框架项目实战
<form id="formData2" method="post" enctype="multipart/form-data">
编&emsp;号:<input type="text" name="id"><br>vectorjava用法
账户名:<input type="text" name="name"><br>
⾦&emsp;额:<input type="text" name="money"><br>
<input type="file" name="upload"><br>
<input id="sub2" type="button" value="上传">
</form>
<hr>
<p>异步上传多个⽂件,且表单有其他数据</p>
<form id="formData3" method="post" enctype="multipart/form-data">
编&emsp;号:<input type="text" name="id"><br>
账户名:<input type="text" name="name"><br>
⾦&emsp;额:<input type="text" name="money"><br>
<input type="file" name="upload" multiple="multiple"><br>
<input id="sub3" type="button" value="上传">
</form>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script>
$(function () {
/
/异步上传⼀个⽂件
$("#sub").click(function () {
var file = new FormData($("#formData")[0]);
$.post({
url:'upload/testUpload',
contentType:false, //jQuery不要去设置Content-Type请求头
processData:false, //jQuery不要去处理发送的数据
cache:false, //不缓存
dataType:'json', //返回类型json
data:file, //⽂件数据
success:function (res) {
console.log(res);
}
});
});
jquery下载的文件怎么使用//异步上传⼀个⽂件,带表单参数
$("#sub2").click(function () {
//将form表单转换为FormData对象
var data = new FormData(document.querySelector("#formData2")); $.post({
url:'upload/testUpload2',
contentType:false, //jQuery不要去设置Content-Type请求头
processData:false, //jQuery不要去处理发送的数据
cache:false, //不缓存
dataType:'json', //返回类型json
data:data, //表单数据
success:function (res) {
console.log(res);
},
error:function (error) {
console.log(error);
}
});
});
/
/异步上传多个⽂件,带表单参数
$("#sub3").click(function () {
//将form表单转换为FormData对象
var data = new FormData(document.querySelector("#formData3")); $.post({
url:'upload/testUpload3',
contentType:false, //jQuery不要去设置Content-Type请求头
processData:false, //jQuery不要去处理发送的数据
cache:false, //不缓存
dataType:'json', //返回类型json
data:data, //表单数据
success:function (res) {
console.log(res);
},
error:function (error) {
console.log(error);
}
});
});
});
</script>
</body>
</html>
下⾯是controller:
@Controller
@RequestMapping("/upload")
public class FileController {
/**
* 同步⽂件⼀个上传和异步上传⼀个⽂件,共同使⽤这⼀个控制器⽅法
* @param request
* @param uploadhtml网页设计代码作业追星
* @return
* @throws IOException
*/
@RequestMapping(value = "/testUpload",method = RequestMethod.POST)
public String upload(HttpServletRequest request, MultipartFile upload) throws IOException {
//获取⽂件的保存路径
String path = ServletContext().getRealPath("/uploads");
//获取上传⽂件的名称
String filename = OriginalFilename();
//获取随机字符串
String prefix = UUID.randomUUID().toString().replaceAll("-", "");
filename = prefix + "_" + filename;
//创建⽂件对象
File file = new File(path);
/
/判断路径是否存在,不存在则创建
if(!ists()){
file.mkdir();
}
//上传⽂件
return "success";
}
/**
* 异步⽂件上传和表单数据
* @param request
* @param upload
* @return
* @throws IOException
*/
@RequestMapping(value = "/testUpload2",method = RequestMethod.POST)
public @ResponseBody Account upload2(HttpServletRequest request, MultipartFile upload, Account account) throws IOException { //获取⽂件的保存路径
String path = ServletContext().getRealPath("/uploads");
//获取上传⽂件的名称
String filename = OriginalFilename();
//获取随机字符串
String prefix = UUID.randomUUID().toString().replaceAll("-", "");
filename = prefix + "_" + filename;
//创建⽂件对象
File file = new File(path);
//判断路径是否存在,不存在则创建
if(!ists()){
file.mkdir();
}
//上传⽂件
return account;
}
/
**
* 异步多个⽂件上传和表单数据
* @param request
* @param upload 采⽤数组接收
* @return
* @throws IOException
*/
@RequestMapping(value = "/testUpload3",method = RequestMethod.POST)
public @ResponseBody Account upload3(HttpServletRequest request, MultipartFile[] upload, Account account) throws IOException { //获取⽂件的保存路径
String path = ServletContext().getRealPath("/uploads");
//创建⽂件对象
File file = new File(path);
//判断路径是否存在,不存在则创建
if(!ists()){
file.mkdir();
}
for (MultipartFile multipartFile : upload) {
//获取上传⽂件的名称
String filename = OriginalFilename();
//获取随机字符串
String prefix = UUID.randomUUID().toString().replaceAll("-", "");
filename = prefix + "_" + filename;
/
/上传⽂件
}
return account;
}
}
public class Account implements Serializable {
private int id;
private String name;
private float money;
//getter
}
注意事项:
上传⽂件时,表单的 enctype 修改为:multipart/form-data;
后端使⽤ MultipartFile upload 对象接收,upload 必须和 <input> 的name属性⼀致;上传多个⽂件,给 <input> 添加:multiple=“multiple”
效果:
更多精彩内容请参考专题,和进⾏学习。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论