vue实现压缩图片预览并上传功能(promise封装)(2)

完成图片压缩后,就可以塞进formdata里进行上传了,先将base64数据转成字符串,再实例化一个ArrayBuffer,然后将字符串以8位整型的格式传入ArrayBuffer,再通过BlobBuilder或者Blob对象,将8位整型的ArrayBuffer转成二进制对象blob,再将blob转为File对象

_upload(data, name, type) { let text = window.atob(data.split(",")[1]); let buffer = new ArrayBuffer(text.length); let ubuffer = new Uint8Array(buffer); let pecent = 0, loop = null; for (var i = 0; i < text.length; i++) { ubuffer[i] = text.charCodeAt(i); } let Builder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder; let blob; if (Builder) { var builder = new Builder(); builder.append(buffer); blob = builder.getBlob(type); } else { blob = new window.Blob([ubuffer], { type: type }); } // blob 转file var fileOfBlob = new File([blob], name, { type: type }); return fileOfBlob; } }

将图片压缩上传封装到一个js文件里

const UploadImg = { imageHandle(files, maxSize, imgDom) { let that = this; let formdata = new FormData(); let reader = new FileReader(); reader.readAsDataURL(files[0]); //将图片转成base64格式 //reader.onload是异步,要用到Promise对象将值返回出去 return new Promise((resolved, rejected) => { reader.onload = function () { let result = this.result; let img = new Image(); img.src = result; if (this.result.length <= maxSize) { imgDom.src = result; img = null; formdata.append("image", that._upload(result, files[0].name, files[0].type)); resolved(formdata); } else { img.onload = function () { let data = that._compress(img); imgDom.src = data; formdata.append("image", that._upload(data, files[0].name, files[0].type)); resolved(formdata); }; } }; }) }, _compress(img) { let canvas = document.createElement("canvas"); let ctx = canvas.getContext("2d"); //瓦片 let tCanvas = document.createElement("canvas"); let tctx = tCanvas.getContext("2d"); let width = img.width; let height = img.height; //如果图片大于四百万像素,计算压缩比并将大小压至400万以下 let ratio; if ((ratio = (width * height) / 4000000) > 1) { ratio = Math.sqrt(ratio); widht /= ratio; height /= ratio; } else { ratio = 1; } canvas.width = width; canvas.height = height; //铺底色 ctx.fillStyle = "#fff"; ctx.fillRect(0, 0, canvas.width, canvas.height); //如果图片像素大于100万则使用瓦片绘制 let count; if ((count = (width * height) / 1000000) > 1) { count = ~~(Math.sqrt(count) + 1); //计算要分成多少瓦片 //计算每块瓦片的宽高 let nw = ~~(width / count); let nh = ~~(height / count); tCanvas.width = nw; tCanvas.height = nh; for (let i = 0; i < count; i++) { for (let j = 0; j < count; j++) { tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh); ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh); } } } else { ctx.drawImage(img, 0, 0, canvas.width, canvas.height); } //进行最小压缩 let ndata = canvas.toDataURL("image/jpeg", 0.3); tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0; return ndata; }, _upload(data, name, type) { let text = window.atob(data.split(",")[1]); let buffer = new ArrayBuffer(text.length); let ubuffer = new Uint8Array(buffer); for (var i = 0; i < text.length; i++) { ubuffer[i] = text.charCodeAt(i); } let Builder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder; let blob; if (Builder) { var builder = new Builder(); builder.append(buffer); blob = builder.getBlob(type); } else { blob = new window.Blob([ubuffer], { type: type }); } // blob 转file var fileOfBlob = new File([blob], name, { type: type }); return fileOfBlob; } } export default UploadImg

调用代码

import UploadImg from "../../util/uploadImg"; methods: { imageHandle(e) { let maxSize = 100 * 1024; let imgDom = this.$refs.upImg; let files = e.srcElement.files; if (!files.length) return; //文件长度大于0 if (!/^image\//.test(files[0].type)) return; //必须是图片才处理 if (!window.FileReader) return; //支持FileReader if (this.docEntry === "" || this.lineId === "") { this.$toast("请填写完整信息", "error"); return; } // let formdata = new FormData(); UploadImg.imageHandle(files, maxSize, imgDom).then(formdata => { formdata.append("docEntry", this.docEntry); formdata.append("lineId", this.lineId); formdata.append("action", "ProductionListImage"); this.$store .dispatch("uploadImage", formdata) .then(data => { if (data === 1) { this.$toast("上传成功", "success"); } else if (data === -1) { this.$toast("图片为空", "error"); } else { this.$toast("上传失败", "error"); } }) .catch(error => this.$toast("上传失败", "error")); }); } }

参考链接:移动端利用H5实现压缩图片上传功能

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/0cdb80c4801d1067dc20c7a3d6de51ac.html