ajax上传文件
发布时间:2020-12-16 03:23:10 所属栏目:百科 来源:网络整理
导读:input type="file" name="file" id="button"/script$( "#button" ).change(function(){ var formdata = new FormData(); formdata.append("file",this.files[0]); var size = this.files[0].size; if (size 10 * 1024 * 1024) { // 判断文件大小 console.log
<input type="file" name="file" id="button"/> <script> $( "#button" ).change(function(){ var formdata = new FormData(); formdata.append("file",this.files[0]); var size = this.files[0].size; if (size > 10 * 1024 * 1024) { // 判断文件大小 console.log('文件最大上传为10M'); return false; }; $.ajax({ type: 'POST',url: '/upload/upload',data: formdata,contentType: false,// 必须false才会自动加上正确的Content-Type processData: false,// 必须false才会避开jQuery对 formdata 的默认处理,XMLHttpRequest会对 formdata 进行正确的处理 success : function(data) { console.log(data); },error : function(data) { console.log('错误'); console.log(data); } }); }) </script> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |