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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| <template> <el-upload class="basic-upload-field__uploader" drag ref="upload" :action="actionUrl" :on-success="uploadSuccess" :before-upload="beforeUpload" :on-remove="removeHandler" :http-request="httpRequest" multiple> <i class="el-icon-upload"></i> <div class="el-upload__text">{{$t('upload.drag')}} <em>{{$t('upload.select')}}</em> </div> <div class="el-upload__tip" slot="tip">{{$t('upload.tip')}}</div> </el-upload>
</template>
<script>
import axios from '~/plugins/axios.js'
export default { name: 'basic-upload-field', data () { return { fileList: [], actionUrl: `${process.env.baseURL}/api/file/upload`, fileReader: '', } },
methods: { httpRequest (options) { let file = options.file let filename = file.name if (file) { this.fileReader.readAsDataURL(file) } this.fileReader.onload = () => { let base64Str = this.fileReader.result let config = { url: '/api/file/upload/base64', method: 'post', // file: file, data: { base64Str: base64Str.split(',')[1], name: filename }, timeout: 10000, onUploadProgress: function (progressEvent) { // console.log(progressEvent) progressEvent.percent = progressEvent.loaded / progressEvent.total * 100 options.onProgress(progressEvent, file) }, } axios(config) .then(res => { options.onSuccess(res, file) }) .catch(err => { options.onError(err) }) } }, removeHandler (file, fileList) { let index = this.fileList.indexOf(file.key) this.fileList.splice(index, 1) console.log('current file list ==>', this.fileList) this.$store.commit('applicant/updateResume', this.fileList) axios.delete(`/api/file/delete?key=${file.key}`).then(res => { console.log(res) }) }, beforeUpload (file) { const isLt5M = file.size < 5 * 1024 * 1024 if (this.fileList.length >= 3) { alert('At most 3 files') return false } if (!isLt5M) { alert('The max size is 5MB') return false } }, uploadSuccess (res, file, fileList) { let data = res.data console.log('upload result:', res, file) file.key = data.key this.fileList.push(data.key) } }, mounted () { if (!window.FileReader) { console.error('Your browser does not support FileReader API!') } this.fileReader = new FileReader() } } </script>
<style > .basic-upload-field { padding: 16px; /* text-align: left; */ } .basic-upload-field__uploader { padding: 16px; text-align: center; } .basic-upload-field__uploader .el-upload-dragger { min-width: 600px; } .basic-upload-field__uploader .el-upload-list { /* text-align: center; */ margin: 0 auto; padding: 0 16px; min-width: 500px; } .basic-upload-field__uploader .el-upload-list__item-name { max-width: 600px; } </style>
|