js前端本地客户端压缩图片后再上传
from;https://github.com/think2011/localResizeIMG
lrz.js
npm install lrz
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | //上传文件 PostFile: function (url, formData) { var instance = axios.create({ timeout: "10000" , headers: { "userid" : getCookie( "userId" ), "corpid" : getCookie( "corpId" ), "Content-Type" : "multipart/form-data" } }); instance.interceptors.response.use( function (response) { console.log(JSON.stringify(response.data), response.headers.errcode) if (JSON.stringify(response.headers) == "{}" ) { vueObj.$children[0].showToast({ message: "上传失败,文件超过6M或网速较慢" , timeout: 2000 }); return Promise.reject(response); } else { return response; } }, function (error) { if (error.response.status == 429 ) { //超时 vueObj.$children[0].showToast({ message: "系统繁忙,请稍后再试!" , timeout: 2000 }); } if (error.code == "ECONNABORTED" || error.message.indexOf( 'timeout' ) > -1 || error.message == 'Network Error' ) { //超时 vueObj.$children[0].showToast({ message: "请求数据超时,请稍后再试!" , timeout: 2000 }); } if (error.response && error.response.status == 500) { if (error.response.headers.errcode > 1000 || [ '107' , '109' , '101' , '102' , '108' , '103' , '105' ].indexOf(error.response.headers.errcode) != -1) { vueObj.$children[0].showToast({ message: decodeURIComponent(error.response.headers.errmsg).replace( new RegExp( "\\+" , "g" ), " " ), timeout: 2000 }); } } return Promise.reject(error); }); console.log(1111) console.log(formData.get( "file" )) return lrz(formData.get( "file" ), { quality: 0.7 //自定义使用压缩方式 }) .then( function (rst) { console.log(rst) console.log(222) var file = new File([rst.file], rst.origin.name, {type: rst.origin.type}); formData.set( "file" ,file); return instance.post(url, formData); }) . catch ( function (error) { //失败时执行 return Promise.reject(error); }) .always( function () { //不管成功或失败,都会执行 }); //return instance.post(url, formData); }, |
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步