uniapp uvew2 upload compressorjs 压缩图片

需求是老师点评,需要上传图片,但是图片太大了,进行压缩,因为是h5 所以自带的压缩不支持,搜了很多文档,结合了一下,最后艰难的完成这个功能,记录一下。

架构是uniapp uview2 上传组件upload,上传代码是uni.uploadFile,后端是gofastdfs的上传代码,主要是把图片从前端压缩后传给后端上传,返回文件地址。

上传组件upload 方法就是uview2官网上的上传例子

<u-upload

:fileList="fileList6"

@afterRead="afterRead"

@delete="deletePic"

name="6"

multiple

:maxCount="6"

:previewFullImage="true"

width="100"

height="100"

:custom-btn="true"

></u-upload>

上传方法

async afterRead(event) {

console.log(event);

if(event.name == 2){

console.log('上传视频功能升级中');

return;

}

// 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式

let lists = [].concat(event.file)

let fileListLen = this[`fileList${event.name}`].length

lists.map((item) => {

this[`fileList${event.name}`].push({

...item,

status: 'uploading',

message: '上传中'

})

})

for (let i = 0; i < lists.length; i++) {

console.log("lists[i].url:")

console.log(lists[i].url)

console.log(lists[i]);

// Blob Url 转Blob

const blod = await this.fetchBlobFromUrl(lists[i].url);

console.log("this is blob:")

console.log(blod) // Blob {size: 1138, type: 'image/png'}

//compressImage 参数是file 或者blob

const comblod = await this.compressImage(blod,lists[i]);

console.log("this is comblod:");

console.log(comblod);

// const result = JSON.parse(await this.uploadFilePromise(lists[i].url))

const result = JSON.parse(await this.uploadFilePromise(comblod,lists[i]))

let item = this[`fileList${event.name}`][fileListLen]

this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {

status: 'success',

message: '',

url: result.data.url,

fileId:result.data.fileId

}))

this.fileIdList.push(result.data.fileId);

fileListLen++

}

}

//Blob Url 转Blob

fetchBlobFromUrl(blobUrl) {

return new Promise((resolve, reject) => {

const xhr = new XMLHttpRequest();

xhr.open('GET', blobUrl, true);

xhr.responseType = 'blob';

xhr.onload = function () {

if (xhr.status === 200) {

const blob = xhr.response;

console.log("blob--->");

console.log(blob);

console.log("xhr.response --->");

console.log(xhr.response);

resolve(blob);

} else {

reject(new Error(`Failed to fetch blob: ${xhr.statusText}`));

}

};

xhr.onerror = function () {

reject(new Error('Network error'));

};

xhr.send();

})

}

// 压缩图片方法

compressImage(filepath,file) {

console.log(filepath);

console.log(file);

const promise = new Promise((resolve, reject) => {

new Compressor(filepath, {

quality: 0.6, // 压缩质量,默认0.6

convertSize: 1024 * 100, // 转换的图片大小,默认1024 * 100,即100kb

success: (result) => {

// 压缩成功后的图片路径result.target为压缩后的图片路径,result.blob为压缩后的图片blob对象,可以用来上传到服务器

console.log('Compressed image path:', result); // 或者上传result.blob到服务器

// this.uploadImage(result.target); // 可以选择上传压缩后的图片或者使用blob对象上传

// let newfile = new File([result],file.name,{type:file.type});

// console.log(newfile)

let url = URL.createObjectURL(result);

resolve(url);

},

error(err) {

console.log(err);

console.error('Something went wrong:');

},

// 其他配置...

});

});

return promise;

},

//上传方法

// uploadFile 这里filepath需要blob url ,后端MultipartFile接收,不压缩后端能接收到filename,压缩后收不到,找不到解决方法为不知道为什么,前端传文件名称

uploadFilePromise(url,file) {

console.log("vuex_token");

const vuex_token = storage.get("AccessToken");

const studentId = this.formData.studentId;

console.log(url);

return new Promise((resolve, reject) => {

let a = uni.uploadFile({

url: '192.168.10.198:81/dev-api/upload/upload/lessonFileUpload', // 仅为示例,非真实的接口地址

filePath: url,

name: 'file',

formData: {

studentId: studentId,

fileName:file.name

},

header: {

Authorization: vuex_token

},

success: (res) => {

setTimeout(() => {

resolve(res.data)

}, 1000)

}

});

})

},

导入类

npm install compressorjs

引入

import Compressor from 'compressorjs';

后端

复制代码
@PostMapping("/lessonFileUpload")
public AjaxResult lessonFileUpload(@RequestParam("file") MultipartFile mulFile,
                                         @RequestParam(value = "title",required = false) String title,
                                         @RequestParam(value = "content",required = false) String content,
                                         @RequestParam(value = "photography",required = false) String photography,
                                         @RequestParam(value = "design",required = false) String design,
                                         @RequestParam(value = "destypeign",required = false) String destypeign,
                                         @RequestParam(value = "type",required = false) String type,
                                         @RequestParam(value = "keyword",required = false) String keyword,
                                         @RequestParam(value = "classId",required = false) String classId,
                                         @RequestParam(value = "deptId",required = false) String deptId,
                                          @RequestParam(value = "studentId",required = false) String studentId,
                                         @RequestParam(value = "fileName",required = false) String fileName

)
相关推荐
游戏开发爱好者81 天前
抓包工具有哪些?代理抓包、数据流抓包、拦截转发工具
android·ios·小程序·https·uni-app·iphone·webview
郑州光合科技余经理1 天前
技术解析:如何打造适应多国市场的海外跑腿平台
java·开发语言·javascript·mysql·spring cloud·uni-app·php
前端 贾公子1 天前
uniapp之实现拖拽排序
uni-app
一室易安1 天前
uniapp vue3 小程序中 手写模仿京东淘宝加入购物车红点曲线飞入样式效果 简化版代码
小程序·uni-app
00后程序员张1 天前
混合 App 怎么加密?分析混合架构下常见的安全风险
android·安全·小程序·https·uni-app·iphone·webview
2501_915921432 天前
Flutter App 到底该怎么测试?如何在 iOS 上进行测试
android·flutter·ios·小程序·uni-app·cocoa·iphone
2501_915909062 天前
如何在 Windows 上上架 iOS App,分析上架流程哪些是不用mac的
android·macos·ios·小程序·uni-app·iphone·webview
2501_915921432 天前
分析 iOS 描述文件创建与管理中常见的问题
android·ios·小程序·https·uni-app·iphone·webview
Aftery的博客2 天前
Uniapp-vue实现语言功能切换(多语言)
javascript·vue.js·uni-app
我这一生如履薄冰~3 天前
uni-app 项目配置代理踩坑
uni-app