传统上传文件的方式是form表单上传,如下
<form id= "uploadForm" action= "http://localhost:8080/cfJAX_RS/rest/file/upload" method= "post" enctype ="multipart/form-data">
<h1 >测试通过Rest接口上传文件 </h1>
<p >指定文件名: <input type ="text" name="filename" /></p>
<p >上传文件: <input type ="file" name="file" /></p>
<p >关键字1: <input type ="text" name="keyword" /></p>
<p >关键字2: <input type ="text" name="keyword" /></p>
<p >关键字3: <input type ="text" name="keyword" /></p>
<input type ="submit" value="上传"/>
</form>
点击上传按钮表单直接上传,这个过程是异步的,如果想同步上传只能借助ajax
实现方式如下
<form id="form1" enctype="multipart/form-data" >
<input type="file" name="uploadfile"/>
</form>
<button id="btn">123</button>
js代码
<script>
$("#btn").click(function(){
$.ajax({
url: 'http://10.80.3.252:9090/excel',
type: 'POST',
cache: false,
data: new FormData($('#form1')[0]),
processData: false,
contentType: false
}).done(function(res) {
}).fail(function(res) {});
})
</script>
即可实现ajax上传form里面的文件,同时可以设置async参数为同步