一、传统表单提交传输方式
<form id= "uploadForm" action= "url" method= "post" enctype ="multipart/form-data">
<h1 >测试通过Rest接口上传文件 </h1>
<p >上传文件: <input type ="file" name="file" /></p>
<p >关键字1: <input type ="text" name="keyword" /></p>
<input type ="submit" value="上传"/>
</form>
二、ajax提交方式传输 (注意红色字体区域 -- 设置enctype)
<form id="uploadForm" enctype="multipart/form-data"> <input id="file" type="file" name="file"/> <button id="upload" type="button">upload</button> </form> <script>
$('#upload').click(function(){
$.ajax({
url: 'upload',
type: 'POST',
cache: false,
data: new FormData($('#uploadForm')[0]),
processData: false, // 不进行数据处理
contentType: false
}).done(function(res) {
}).fail(function(res) {});
});
</script>
三、关于ios对 new FormData 兼容
苹果部分机型(如iphone6),new FormData 无法使用set/get方法,只能使用append方法设置值。
