1、最近移动端项目中使用van-uploader上传图片,开发测试都正常,上线后苹果用户反馈图片上传不了,最后排查是图片过大导致调用上传接口后一直没有返回。
2、utils文件夹下新建imgCompressUtil.js文件:
3、工具类中的具体代码:
const imgCompressUtil = {
compressImg (file) {
let self = this
if (!file || !window.FileReader) return // 判断是否支持FileReader
if (/^image/.test(file.type)) {
let reader = new FileReader()
reader.readAsDataURL(file) // 转成 base64 格式
return new Promise((resolve, reject) => { // 读取成功后的回调
reader.onloadend = function () {
let img = new Image()
img.src = this.result
// 判断图片是否大于500K,是就直接上传,反之压缩图片
if (this.result.length <= 500 * 1024) {
resolve(this.result)
} else {
img.onload = function () {
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) {
console.log('大于400万像素')
ratio = Math.sqrt(ratio)
width /= 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, width, height)
}
// 进行压缩(---------quailty越低 图片越小)
let quailty = 0.5
let newBase64 = canvas.toDataURL('image/jpeg', quailty)
tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0
resolve(newBase64) // 返回压缩后的base64
}
}
}
})
}
}
}
export default imgCompressUtil
4、van-uploader上传图片的内容如下,直接上传的话用content(base64),压缩的话压缩其中的file文件即可:
5、文件压缩:
async submit() {
Toast.loading({
duration: 0,
forbidClick: true,
message: "任务反馈中..."
})
let files = []
for (let i = 0; i < this.fileList.length; i++) {
await imgCompressUtil.compressImg(this.fileList[i].file).then(res => {
files.push(res)
})
}
let params = {
id: this.item.id,
content: this.contentValue,
files: files,
meritId: this.item.meritsId,
userId: this.userid
}
commonApi.feedback(params).then(res => {
Toast.clear()
if (res.code === '0') {
Toast.success('任务反馈成功')
this.$router.go(-1)
} else {
this.$message({ message: res.msg, type: 'warning', duration: 2000 })
}
})
}
上面代码中fileList中便是上传的图片集合,压缩时要用async、await,压缩后再上传便正常了。