1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
uploadImg (event) { if (!event.target.files[0]) return; let file = event.target.files[0], fileName = file.name.substring(file.name.lastIndexOf(".") + 1).toLowerCase(); if (fileName != "jpg" && fileName != "png" && fileName != "gif" ) { this.$store.commit('setPrompt', {status: true, text: '请选择正确的图片格式上传(jpg,png,gif)'}) return } if (fileName == 'gif') { this.uploadApi(file, file.name) } else { let _this = this; let reader = new FileReader(), img = new Image(); reader.readAsDataURL(file); let canvas = document.createElement('canvas'); let context = canvas.getContext('2d'); img.onload = function() { let originWidth = this.width; let originHeight = this.height; canvas.width = originWidth; canvas.height = originHeight; context.clearRect(0, 0, originWidth, originHeight); context.drawImage(img, 0, 0, originWidth, originHeight); canvas.toBlob(function(blob) { _this.uploadApi(blob, file.name) }, file.type == 'image/gif' ? 'image/gif' : "image/jpeg", 0.6); } reader.onload = function(e) { img.src = e.target.result; }; } },
uploadApi(file, fileName) { let formData = new FormData() formData.append('img', file, fileName) axios.post('/upload/upload-img', formData, {headers: {'Content-Type': 'multipart/form-data'}}).then((res) => { if (res.data.code == 200) { this.img_list.splice(this.img_list.length, 1, res.data.url) } else { this.$store.commit('setPrompt', {status: true, text: res.data.message}) } }) }
|