设为首页 加入收藏

TOP

土旦:移动端 Vue+Vant 的Uploader 实现 :上传、压缩、旋转图片(二)
2019-09-17 19:00:46 】 浏览:68
Tags:土旦 移动 Vue Vant Uploader 实现 上传 压缩 旋转 图片
const min_step = 0; const max_step = 3; if (img == null) return; //img的高度和宽度不能在img元素隐藏后获取,否则会出错 let height = img.height; let width = img.width; let step = 2; if (step == null) { step = min_step; } if (direction == "right") { step++; //旋转到原位置,即超过最大值 step > max_step && (step = min_step); } else { step--; step < min_step && (step = max_step); } //旋转角度以弧度值为参数 let degree = (step * 90 * Math.PI) / 180; let ctx = canvas.getContext("2d"); switch (step) { case 0: canvas.width = width; canvas.height = height; ctx.drawImage(img, 0, 0); break; case 1: canvas.width = height; canvas.height = width; ctx.rotate(degree); ctx.drawImage(img, 0, -height); break; case 2: canvas.width = width; canvas.height = height; ctx.rotate(degree); ctx.drawImage(img, -width, -height); break; case 3: canvas.width = height; canvas.height = width; ctx.rotate(degree); ctx.drawImage(img, -width, 0); break; } }, //将base64转换为文件 dataURLtoFile(dataurl) { var arr = dataurl.split(","), bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new File([u8arr], this.files.name, { type: this.files.type }); }, //这里写接口 async postImg() { let file = this.dataURLtoFile(this.headerImage); let formData = new window.FormData(); formData.append("file", file); toast_loding(this, "图片上传中···"); try { let res = await util.ajax.post(this.upImgUrl, formData, { headers: { "Content-Type": "multipart/form-data" } }); } catch (e) { console.log(e); } }

 

记录走过的路,踩过的坑,互勉。

  前端交流群:87709616

有不同意见的可以留言,我们一起讨论。

 

参考:https://www.cnblogs.com/lvshaonan/p/8547676.html

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Promise详解 下一篇Ajax方式上传文件报错"Uncau..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目