Commit f73c667b authored by chenqikuai's avatar chenqikuai

fix:上传图片前压缩图片

parent be2ba058
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
"@types/echarts": "^4.9.10", "@types/echarts": "^4.9.10",
"ant-design-vue": "^1.7.7", "ant-design-vue": "^1.7.7",
"axios": "^0.21.1", "axios": "^0.21.1",
"compressorjs": "^1.1.1",
"core-js": "^3.6.5", "core-js": "^3.6.5",
"echarts": "^5.2.0", "echarts": "^5.2.0",
"element-ui": "^2.15.5", "element-ui": "^2.15.5",
...@@ -60,4 +61,4 @@ ...@@ -60,4 +61,4 @@
"last 2 versions", "last 2 versions",
"not dead" "not dead"
] ]
} }
\ No newline at end of file
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
action="/proxyApi/api/v1/image/upload" action="/proxyApi/api/v1/image/upload"
:accept="ACCEPT_IMAGE_TYPE" :accept="ACCEPT_IMAGE_TYPE"
:before-upload="beforeUpload" :before-upload="beforeUpload"
:transformFile="transformFile"
@change="handleChange" @change="handleChange"
> >
<img v-if="imageUrl" :src="imageUrl" /> <img v-if="imageUrl" :src="imageUrl" />
...@@ -32,6 +33,7 @@ import { ...@@ -32,6 +33,7 @@ import {
TOAST_TEXT2, TOAST_TEXT2,
MAX_IMAGE_SIZE, MAX_IMAGE_SIZE,
} from '@/const/config/upload' } from '@/const/config/upload'
import compress from '@/utils/image/compress';
function getBase64(img: Blob, callback: (any: any) => void) { function getBase64(img: Blob, callback: (any: any) => void) {
...@@ -64,6 +66,10 @@ export default Vue.extend({ ...@@ -64,6 +66,10 @@ export default Vue.extend({
} }
}, },
methods:{ methods:{
transformFile(file: File){
console.log('transformFile');
return compress(file)
},
handleChange(info:any) { handleChange(info:any) {
if (info.file.status === 'uploading') { if (info.file.status === 'uploading') {
this.loading = true; this.loading = true;
...@@ -84,11 +90,11 @@ export default Vue.extend({ ...@@ -84,11 +90,11 @@ export default Vue.extend({
if (!isAccpet) { if (!isAccpet) {
message.error(TOAST_TEXT) message.error(TOAST_TEXT)
} }
const isLt4M = file.size < MAX_IMAGE_SIZE const isLessThenMaxImageSize = file.size < MAX_IMAGE_SIZE
if (!isLt4M) { if (!isLessThenMaxImageSize) {
message.error(TOAST_TEXT2) message.error(TOAST_TEXT2)
} }
return isAccpet && isLt4M return isAccpet && isLessThenMaxImageSize
}, },
} }
}) })
......
export const ACCEPT_IMAGE_TYPE = '.jpg,.jpep,.png' export const ACCEPT_IMAGE_TYPE = '.jpg,.jpeg,.png'
export function isAcceptImageType(file: File) { export function isAcceptImageType(file: File) {
const isCorrectType = file.name.match(/(\.jpg|\.jpep|\.png)$/) const isCorrectType = file.name.match(/(\.jpg|\.jpeg|\.png)$/)
return !!isCorrectType return !!isCorrectType
} }
export const TOAST_TEXT = '请上传jpg或jpep或png文件' export const TOAST_TEXT = '请上传jpg或jpeg或png文件'
// 最大上传大小 40M export const MAX_IMAGE_SIZE = 1024 * 1024 * 12
export const MAX_IMAGE_SIZE = 1024 * 1024 * 40
export const TOAST_TEXT2 = '请上传小于40M的图片' export const TOAST_TEXT2 = `请上传小于${MAX_IMAGE_SIZE / 1024 / 1024}M的图片`
import compress from '@/utils/image/compress'
import baseAxios from '../index' import baseAxios from '../index'
export default class FileService { export default class FileService {
static instance: FileService static instance: FileService
...@@ -33,12 +34,14 @@ export default class FileService { ...@@ -33,12 +34,14 @@ export default class FileService {
} }
uploadImage(uploadFile: File) { uploadImage(uploadFile: File) {
const fd = new FormData() return compress(uploadFile).then((ret) => {
fd.append('uploadFile', uploadFile) const fd = new FormData()
return baseAxios<string>({ fd.append('uploadFile', ret)
method: 'post', return baseAxios<string>({
url: '/image/upload', method: 'post',
data: fd, url: '/image/upload',
data: fd,
})
}) })
} }
} }
import Compressor from 'compressorjs'
export default function (file: Blob | File): Promise<File | Blob> {
return new Promise((resolve, reject) => {
new Compressor(file, {
quality: 0.6,
success: (result) => {
if (file instanceof File) resolve(new File([result], file.name))
else resolve(result)
},
error: (result) => {
reject(result)
},
})
})
}
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<p class="text-2xl font-bold mb-5">{{ title }}</p> <p class="text-2xl font-bold mb-5">{{ title }}</p>
{{ $route.query.id }} {{ $route.query.id }}
<p class="text-gray-400">操作时间:{{ time | formatDate }}</p> <p class="text-gray-400">操作时间:{{ time | formatDate }}</p>
<img v-if="imageUrl" :src="imageUrl" class="mx-auto my-5" /> <img v-if="imageUrl" :src="imageUrl" class="mx-auto my-5 max-h-64" />
<div class="text-left p-5 el-tiptap-editor editor" v-html="content"> <div class="text-left p-5 el-tiptap-editor editor" v-html="content">
{{ content }} {{ content }}
</div> </div>
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
<a-form-model-item label="上传图片" :required="true" prop="fileList"> <a-form-model-item label="上传图片" :required="true" prop="fileList">
<a-upload <a-upload
:beforeUpload="beforeUpload" :beforeUpload="beforeUpload"
:transformFile="transformFile"
name="uploadFile" name="uploadFile"
action="/proxyApi/api/v1/image/upload" action="/proxyApi/api/v1/image/upload"
:accept="ACCEPT_IMAGE_TYPE" :accept="ACCEPT_IMAGE_TYPE"
...@@ -154,6 +155,7 @@ import { PAGE_SIZE } from '@/const/config/page' ...@@ -154,6 +155,7 @@ import { PAGE_SIZE } from '@/const/config/page'
import { iBannerItem } from '@/service/BannerManagementService/types' import { iBannerItem } from '@/service/BannerManagementService/types'
import { eBannerStatus } from '@/types/banner' import { eBannerStatus } from '@/types/banner'
import FileService from '@/service/FileService/index' import FileService from '@/service/FileService/index'
import compress from '@/utils/image/compress'
export default Vue.extend({ export default Vue.extend({
components: { components: {
...@@ -296,18 +298,21 @@ export default Vue.extend({ ...@@ -296,18 +298,21 @@ export default Vue.extend({
} catch (err) {} } catch (err) {}
this.modalLoading = false this.modalLoading = false
}, },
transformFile(file: File){
return compress(file)
},
beforeUpload(file: File) { beforeUpload(file: File) {
const isAccpet = isAcceptImageType(file) const isAccpet = isAcceptImageType(file)
if (!isAccpet) { if (!isAccpet) {
message.error(TOAST_TEXT) message.error(TOAST_TEXT)
} }
const isLt4M = file.size < MAX_IMAGE_SIZE const isLessThenMaxImageSize = file.size < MAX_IMAGE_SIZE
if (!isLt4M) { if (!isLessThenMaxImageSize) {
message.error(TOAST_TEXT2) message.error(TOAST_TEXT2)
} }
console.log(isAccpet && isLt4M) console.log(isAccpet && isLessThenMaxImageSize)
return isAccpet && isLt4M return isAccpet && isLessThenMaxImageSize
}, },
handleChange(info: any) { handleChange(info: any) {
console.log(info, 'show info') console.log(info, 'show info')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment