1. 程式人生 > 實用技巧 >Element-ui 上傳el-upload元件的使用方法

Element-ui 上傳el-upload元件的使用方法

最近使用element-ui做了一次上傳,element-ui上傳是單個檔案上傳,每上傳一個檔案就請求一次ajax

程式碼如下

  <el-upload
        accept="doc"     //允許上傳的檔案型別
        ref="upload"
        class="upload-demo"
        :show-file-list="true"    //是否顯示上傳的檔案列表
        multiple               //支援多選
        name="files"             //上傳檔案的鍵名
        :before
-upload="beforeUpload" //上傳前的方法 :on-progress="onProgress" //上傳進度 :data="{token:token}" //上傳引數 :on-error="error" :before-remove="beforeRemove" //刪除檔案前 :file-list="fileList" //檔案列表 :on-success="uploadSuccess" //上傳成功 action="https://data.17.yidu.cc/upload_doc.php"> <p >點選此處新增你要上傳的文件</p> <p>按住Ctrl可以選擇多篇上傳</p> </el-upload>

對應的方法如下:

        //上傳前   
            beforeUpload(file){
console.log(file) //當前上傳的檔案 },
//上傳時 onProgress(event,file){ }, beforeRemove(){ return false; //禁止刪除 }, error(response, file, fileList){
//上傳失敗 }, // 上傳成功 uploadSuccess(response, file, fileList){ if(response.code==200){ //上傳成功 }else { //上傳失敗 刪除改檔案,不在列表中顯示 fileList.splice(fileList.indexOf(file),1); } },