js 介面: 調用: 具體詳見官方api,很詳細 http://element.eleme.io/#/zh-CN/component/upload#attribute ...
<el-upload class="upload-demo" action="" :before-remove="beforeRemove" :on-remove="onRemove" multiple :limit="1" :on-exceed="handleExceed" :file-list="fileList" :http-request="uploadSectionFile"> <el-button size="small" type="primary">點擊上傳</el-button> <!-- <div slot="tip" class="el-upload__tip">只能上傳jpg/png文件,且不超過500kb</div> --> </el-upload>
js
介面:
export const uploadFile = params => { return axios.post(`${easykp_config.test}/file/upload`, params, { headers: { 'author-token-key': localStorage.getItem('token') } }).then(res => res.data); };
調用:
//文件上傳 handleExceed(files, fileList) { //超出限制 this.$message.warning(`當前限制選擇 1 個文件,本次選擇了 ${files.length} 個文件,共選擇了 ${files.length + fileList.length} 個文件`); }, beforeRemove(file, fileList) { //確定移除 return this.$confirm(`確定移除 ${ file.name }?`); }, onRemove(file, fileList) { //刪除清空 // console.log(file, fileList) this.filesId = []; }, uploadSectionFile(param) { var fileObj = param.file; var formData = new FormData(); formData.append('file', fileObj); // console.log('formData',fileObj.size)可做大小限制等 uploadFile(formData).then((res) => { //請求數據 if (res.code == 200) { this.filesId.push(res.body.id); this.$message.success(`上傳成功`); } else { this.$message({ message: res.message, type: 'error' }); } }); }
具體詳見官方api,很詳細
http://element.eleme.io/#/zh-CN/component/upload#attribute