upload上傳是前端開發很經常使用的一個功能,在Vue開發中經常使用的Element組件庫也提供了很是好用的upload組件。前端
代碼:數組
<el-upload :action="uploadActionUrl"> <el-button size="small" type="primary">點擊上傳</el-button> </el-upload>
這個基本不用說,:action是執行上傳動做的後臺接口,el-button是觸發上傳的按鈕。app
首先是設置是否能夠同時選中多個文件上傳,這個也是<input type='file'>的屬性,添加multiple便可。另外el-upload組件提供了:limit屬性來設置最多能夠上傳的文件數量,超出此數量後選擇的文件是不會被上傳的。:on-exceed綁定的方法則是處理超出數量後的動做。代碼以下:this
<el-upload :action="uploadActionUrl" multiple :limit="3" :on-exceed="handleExceed"> <el-button size="small" type="primary">點擊上傳</el-button> </el-upload>
若是須要限制上傳文件的格式,須要添加accept屬性,這個是直接使用<input type='file'>同樣的屬性了,accept屬性的值能夠是accept="image/gif, image/jpeg, text/plain, application/pdf"等等。文件格式的提示,則可使用slot。代碼以下:spa
<el-upload :action="uploadActionUrl" accept="image/jpeg,image/gif,image/png" multiple :limit="3" :on-exceed="handleExceed"> <el-button size="small" type="primary">點擊上傳</el-button> <div slot="tip" class="el-upload__tip">請上傳圖片格式文件</div> </el-upload>
注意這裏只是選擇文件時限制格式,其實用戶仍是能夠點選「全部文件」選項,上傳其餘格式。若是須要在在上傳時再次校驗,擇須要在:before-upload這個鉤子綁定相應的方法來校驗,代碼以下:code
<el-upload :action="uploadActionUrl" accept="image/jpeg,image/gif,image/png" :before-upload="onBeforeUpload" multiple :limit="3" :on-exceed="handleExceed"> <el-button size="small" type="primary">點擊上傳</el-button> <div slot="tip" class="el-upload__tip">請上傳圖片格式文件</div> </el-upload> ... onBeforeUpload(file) { const isIMAGE = file.type === 'image/jpeg'||'image/gif'||'image/png'; const isLt1M = file.size / 1024 / 1024 < 1; if (!isIMAGE) { this.$message.error('上傳文件只能是圖片格式!'); } if (!isLt1M) { this.$message.error('上傳文件大小不能超過 1MB!'); } return isIMAGE && isLt1M; }
這裏在限制文件格式的同時,也作了文件大小的校驗。對象
這裏直接搬運官網的說明了,如圖:blog
這個功能能夠說很強大了,能夠很清晰的顯示已上傳的文件列表,而且能夠方便的刪除,以便上傳新的文件。
代碼:接口
<el-upload :action="uploadActionUrl" accept="image/jpeg,image/gif,image/png" multiple :limit="3" :on-exceed="handleExceed" :on-error="uploadError" :on-success="uploadSuccess" :on-remove="onRemoveTxt" :before-upload="onBeforeUpload" :file-list="files"> <el-button size="small" type="primary">點擊上傳</el-button> <div slot="tip" class="el-upload__tip">請上傳圖片格式文件</div> </el-upload>
實現方法就是:file-list="files"這個屬性的添加,其中files是綁定的數組對象,初始爲空。
效果以下圖:圖片
上傳文件同時須要提交數據給後臺接口,這時就要用到:data屬性。
<el-upload :action="uploadActionUrl" accept="image/jpeg,image/gif,image/png" :data="uploadData" multiple :limit="3" :on-exceed="handleExceed" :on-error="uploadError" :on-success="uploadSuccess" :on-remove="onRemoveTxt" :before-upload="onBeforeUpload" :file-list="files"> <el-button size="small" type="primary">點擊上傳</el-button> <div slot="tip" class="el-upload__tip">請上傳圖片格式文件</div> </el-upload> ... uploadData: { dataType: "0", oldFilePath:"" }
有時咱們須要把選取和上傳分開處理,好比上傳圖片,先選取文件提交到前端,圖片處理後再把base64內容提交到後臺。
代碼以下:
<el-upload action="" accept="image/jpeg,image/png" :on-change="onUploadChange" :auto-upload="false" :show-file-list="false"> <el-button slot="trigger" size="small" type="primary">選取</el-button> <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上傳</el-button> <div slot="tip" class="el-upload__tip">只能上傳jpg/png文件,且不能超過1m</div> </el-upload> ... submitUpload() { console.log("submit") }, onUploadChange(file) { const isIMAGE = (file.raw.type === 'image/jpeg' || file.raw.type === 'image/png'); const isLt1M = file.size / 1024 / 1024 < 1; if (!isIMAGE) { this.$message.error('只能上傳jpg/png圖片!'); return false; } if (!isLt1M) { this.$message.error('上傳文件大小不能超過 1MB!'); return false; } var reader = new FileReader(); reader.readAsDataURL(file.raw); reader.onload = function(e){ console.log(this.result)//圖片的base64數據 } }
效果如圖: