12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- import { api_uploadAttachment } from "@/http/http.js"
- import Compressor from 'compressorjs';
- const toBlob = (canvas, imgObj) => {
- return new Promise((resolve,reject) => {
- canvas.toBlob((fileSrc) => {
- resolve(fileSrc)
- }, `${imgObj.fileType}/${imgObj.extname}`, 0.3)
- })
- }
- const newImage = (img) => {
- return new Promise((resolve,reject) => {
- img.onload = () => {
- resolve()
- }
- })
- }
- const compressImage = (file) => {
- return new Promise((resolve,reject) => {
- new Compressor(file, {
- quality: 0.6,
- maxWidth: 1500,
- maxHeight: 1500,
- success: (result) => {
- resolve(result);
- },
- error: (err) => {
- reject(err);
- }
- });
- })
- }
- export const uploadFile = async (imgObj, type, incidentId) => {
- console.log('44444', type)
- if(imgObj.extname=='mp4' || imgObj.extname=='avi' || imgObj.extname=='mpeg' ||
- imgObj.extname=='wmv' || imgObj.extname=='mov' || imgObj.extname=='3gp' ||
- imgObj.extname=='flv' || imgObj.extname=='mkv'){
- return uni.uploadFile({
- url: api_uploadAttachment(type, incidentId),
- filePath: imgObj.path,
- name: 'file',
- formData: {
- 'filename': imgObj.name
- }
- })
- }else{
- const res1 = await uni.getImageInfo({src: imgObj.url});
- const res = res1[1];
- console.log('压缩前', res)
- let canvasWidth = res.width
- let canvasHeight = res.height
- let img = new Image()
- img.src = res.path
- let canvas = document.createElement('canvas');
- let ctx = canvas.getContext('2d')
- canvas.width = canvasWidth
- canvas.height = canvasHeight
- await newImage(img)
- ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight)
- const blob = await toBlob(canvas, imgObj)
- const fileSrc = await compressImage(blob)
- let tp = window.URL.createObjectURL(fileSrc)
- console.log('压缩后', tp);
- return uni.uploadFile({
- timeout: 600000,
- url: api_uploadAttachment(type, incidentId),
- filePath: tp,
- name: 'file',
- formData: {
- 'filename': imgObj.name
- }
- });
- }
- }
|