Compressor.js前端图片压缩
2022-08-01 18:40:13
2025-01-15 22:31:26
引用方法
Vue3
shell
npm i -S compressorjs
shell
import Compressor from 'compressorjs';
使用方法
js
import axios from 'axios';
import Compressor from 'compressorjs';
document.getElementById('file').addEventListener('change', (e) => {
const file = e.target.files[0];
if (!file) {
return;
}
new Compressor(file, {
quality: 0.6,
success(result) {
const formData = new FormData();
formData.append('file', result, result.name);
axios.post('/path/to/upload', formData).then(() => {
console.log('Upload success');
});
},
error(err) {
console.log(err.message);
},
});
});
常见问题
目录