經過點擊或者拖拽上傳文件web
經過 slot 你能夠傳入自定義的上傳按鈕類型和文字提示。可經過設置limit
和on-exceed
來限制上傳文件的個數和定義超出限制時的行爲。可經過設置before-remove
來阻止文件移除操做。json
1 <el-upload 2 class="upload-demo" 3 action="https://jsonplaceholder.typicode.com/posts/" 4 :on-preview="handlePreview" 5 :on-remove="handleRemove" 6 :before-remove="beforeRemove" 7 multiple 8 :limit="3" 9 :on-exceed="handleExceed" 10 :file-list="fileList"> 11 <el-button size="small" type="primary">點擊上傳</el-button> 12 <div slot="tip" class="el-upload__tip">只能上傳jpg/png文件,且不超過500kb</div> 13 </el-upload> 14 <script> 15 export default { 16 data() { 17 return { 18 fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}] 19 }; 20 }, 21 methods: { 22 handleRemove(file, fileList) { 23 console.log(file, fileList); 24 }, 25 handlePreview(file) { 26 console.log(file); 27 }, 28 handleExceed(files, fileList) { 29 this.$message.warning(`當前限制選擇 3 個文件,本次選擇了 ${files.length} 個文件,共選擇了 ${files.length + fileList.length} 個文件`); 30 }, 31 beforeRemove(file, fileList) { 32 return this.$confirm(`肯定移除 ${ file.name }?`); 33 } 34 } 35 } 36 </script>
使用 before-upload
限制用戶上傳的圖片格式和大小。服務器
1 <el-upload 2 class="avatar-uploader" 3 action="https://jsonplaceholder.typicode.com/posts/" 4 :show-file-list="false" 5 :on-success="handleAvatarSuccess" 6 :before-upload="beforeAvatarUpload"> 7 <img v-if="imageUrl" :src="imageUrl" class="avatar"> 8 <i v-else class="el-icon-plus avatar-uploader-icon"></i> 9 </el-upload> 10 11 <style> 12 .avatar-uploader .el-upload { 13 border: 1px dashed #d9d9d9; 14 border-radius: 6px; 15 cursor: pointer; 16 position: relative; 17 overflow: hidden; 18 } 19 .avatar-uploader .el-upload:hover { 20 border-color: #409EFF; 21 } 22 .avatar-uploader-icon { 23 font-size: 28px; 24 color: #8c939d; 25 width: 178px; 26 height: 178px; 27 line-height: 178px; 28 text-align: center; 29 } 30 .avatar { 31 width: 178px; 32 height: 178px; 33 display: block; 34 } 35 </style> 36 37 <script> 38 export default { 39 data() { 40 return { 41 imageUrl: '' 42 }; 43 }, 44 methods: { 45 handleAvatarSuccess(res, file) { 46 this.imageUrl = URL.createObjectURL(file.raw); 47 }, 48 beforeAvatarUpload(file) { 49 const isJPG = file.type === 'image/jpeg'; 50 const isLt2M = file.size / 1024 / 1024 < 2; 51 52 if (!isJPG) { 53 this.$message.error('上傳頭像圖片只能是 JPG 格式!'); 54 } 55 if (!isLt2M) { 56 this.$message.error('上傳頭像圖片大小不能超過 2MB!'); 57 } 58 return isJPG && isLt2M; 59 } 60 } 61 } 62 </script>
使用 list-type
屬性來設置文件列表的樣式。cookie
1 <el-upload 2 action="https://jsonplaceholder.typicode.com/posts/" 3 list-type="picture-card" 4 :on-preview="handlePictureCardPreview" 5 :on-remove="handleRemove"> 6 <i class="el-icon-plus"></i> 7 </el-upload> 8 <el-dialog :visible.sync="dialogVisible"> 9 <img width="100%" :src="dialogImageUrl" alt=""> 10 </el-dialog> 11 <script> 12 export default { 13 data() { 14 return { 15 dialogImageUrl: '', 16 dialogVisible: false 17 }; 18 }, 19 methods: { 20 handleRemove(file, fileList) { 21 console.log(file, fileList); 22 }, 23 handlePictureCardPreview(file) { 24 this.dialogImageUrl = file.url; 25 this.dialogVisible = true; 26 } 27 } 28 } 29 </script>
1 <el-upload 2 class="upload-demo" 3 action="https://jsonplaceholder.typicode.com/posts/" 4 :on-preview="handlePreview" 5 :on-remove="handleRemove" 6 :file-list="fileList2" 7 list-type="picture"> 8 <el-button size="small" type="primary">點擊上傳</el-button> 9 <div slot="tip" class="el-upload__tip">只能上傳jpg/png文件,且不超過500kb</div> 10 </el-upload> 11 <script> 12 export default { 13 data() { 14 return { 15 fileList2: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}] 16 }; 17 }, 18 methods: { 19 handleRemove(file, fileList) { 20 console.log(file, fileList); 21 }, 22 handlePreview(file) { 23 console.log(file); 24 } 25 } 26 } 27 </script>
經過 on-change
鉤子函數來對列表進行控制ide
1 <el-upload 2 class="upload-demo" 3 action="https://jsonplaceholder.typicode.com/posts/" 4 :on-change="handleChange" 5 :file-list="fileList3"> 6 <el-button size="small" type="primary">點擊上傳</el-button> 7 <div slot="tip" class="el-upload__tip">只能上傳jpg/png文件,且不超過500kb</div> 8 </el-upload> 9 <script> 10 export default { 11 data() { 12 return { 13 fileList3: [{ 14 name: 'food.jpeg', 15 url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100', 16 status: 'finished' 17 }, { 18 name: 'food2.jpeg', 19 url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100', 20 status: 'finished' 21 }] 22 }; 23 }, 24 methods: { 25 handleChange(file, fileList) { 26 this.fileList3 = fileList.slice(-3); 27 } 28 } 29 } 30 </script>
1 <el-upload 2 class="upload-demo" 3 drag 4 action="https://jsonplaceholder.typicode.com/posts/" 5 multiple> 6 <i class="el-icon-upload"></i> 7 <div class="el-upload__text">將文件拖到此處,或<em>點擊上傳</em></div> 8 <div class="el-upload__tip" slot="tip">只能上傳jpg/png文件,且不超過500kb</div> 9 </el-upload>
1 <el-upload 2 class="upload-demo" 3 ref="upload" 4 action="https://jsonplaceholder.typicode.com/posts/" 5 :on-preview="handlePreview" 6 :on-remove="handleRemove" 7 :file-list="fileList" 8 :auto-upload="false"> 9 <el-button slot="trigger" size="small" type="primary">選取文件</el-button> 10 <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上傳到服務器</el-button> 11 <div slot="tip" class="el-upload__tip">只能上傳jpg/png文件,且不超過500kb</div> 12 </el-upload> 13 <script> 14 export default { 15 data() { 16 return { 17 fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}] 18 }; 19 }, 20 methods: { 21 submitUpload() { 22 this.$refs.upload.submit(); 23 }, 24 handleRemove(file, fileList) { 25 console.log(file, fileList); 26 }, 27 handlePreview(file) { 28 console.log(file); 29 } 30 } 31 } 32 </script>
參數 | 說明 | 類型 | 可選值 | 默認值 |
---|---|---|---|---|
action | 必選參數,上傳的地址 | string | — | — |
headers | 設置上傳的請求頭部 | object | — | — |
multiple | 是否支持多選文件 | boolean | — | — |
data | 上傳時附帶的額外參數 | object | — | — |
name | 上傳的文件字段名 | string | — | file |
with-credentials | 支持發送 cookie 憑證信息 | boolean | — | false |
show-file-list | 是否顯示已上傳文件列表 | boolean | — | true |
drag | 是否啓用拖拽上傳 | boolean | — | false |
accept | 接受上傳的文件類型(thumbnail-mode 模式下此參數無效) | string | — | — |
on-preview | 點擊已上傳的文件連接時的鉤子, 能夠經過 file.response 拿到服務端返回數據 | function(file) | — | — |
on-remove | 文件列表移除文件時的鉤子 | function(file, fileList) | — | — |
on-success | 文件上傳成功時的鉤子 | function(response, file, fileList) | — | — |
on-error | 文件上傳失敗時的鉤子 | function(err, file, fileList) | — | — |
on-progress | 文件上傳時的鉤子 | function(event, file, fileList) | — | — |
on-change | 文件狀態改變時的鉤子,添加文件、上傳成功和上傳失敗時都會被調用 | function(file, fileList) | — | — |
before-upload | 上傳文件以前的鉤子,參數爲上傳的文件,若返回 false 或者返回 Promise 且被 reject,則中止上傳。 | function(file) | — | — |
before-remove | 刪除文件以前的鉤子,參數爲上傳的文件和文件列表,若返回 false 或者返回 Promise 且被 reject,則中止上傳。 | function(file, fileList) | — | — |
list-type | 文件列表的類型 | string | text/picture/picture-card | text |
auto-upload | 是否在選取文件後當即進行上傳 | boolean | — | true |
file-list | 上傳的文件列表, 例如: [{name: 'food.jpg', url: 'https://xxx.cdn.com/xxx.jpg'}] | array | — | [] |
http-request | 覆蓋默認的上傳行爲,能夠自定義上傳的實現 | function | — | — |
disabled | 是否禁用 | boolean | — | false |
limit | 最大容許上傳個數 | number | — | — |
on-exceed | 文件超出個數限制時的鉤子 | function(files, fileList) | — | - |
方法名 | 說明 | 參數 |
---|---|---|
clearFiles | 清空已上傳的文件列表(該方法不支持在 before-upload 中調用) | — |
abort | 取消上傳請求 | ( file: fileList 中的 file 對象 ) |