批量手動上傳文件,和表單數據一起提交
1.在el-upload組件關鍵的鈎子,其它省略
multiple
:auto-upload = "false"
:file-list = "fileList"
:on-change = "selectFile" (里面是把上傳的fileList傳給自定義的 this.fileList)
2.點擊上傳,將多個文件和表單數據一起上傳
a.定義FormData,將表單數據和文件填充到FormData里面
b.自定義請求頭部,Content-type:‘multipart/form-data’
let formData = new FormData() for(let key in data){ if(data[key]){ formData.append(key,data[key]) } } this.fileList.forEach((element,i) =>{ formData.append('fileList',element.raw) }) let rs = await axios({ method:'post', url: cfg.uploadURL + '/sp/addSp', data: formData, headers:{ 'Content-type':'multipart/form-data' } })
表格中上傳文件中,組件鈎子函數自帶參數
<el-upload class="upload-demo" action="https://jsonplaceholder.typicode.com/posts/" :on-preview="handlePreview" :on-remove="handleRemove" :before-remove="(file,fileList)=>{ return beforeRemove(file,fileList,scope.$index) }" multiple :limit="3" :on-exceed="handleExceed" :file-list="fileList"> <el-button size="small" type="primary">點擊上傳</el-button> <div slot="tip" class="el-upload__tip">只能上傳jpg/png文件,且不超過500kb</div> </el-upload> beforeRemove(file,fileList,index){ console.log(index) }
批量下載(后台不返回壓縮包,前端就一個個下載了)
download(val){ let vals = val.split(',') //后台返回的文件標識符數組 vals.forEach((element) =>{ const iframe = document.createElement("iframe"); iframe.style.display = "none"; iframe.style.height = 0; iframe.src = cfg.baseURL+'下載路徑'+element; document.body.appendChild(iframe); setTimeout(()=>{ iframe.remove(); }, 1000) }) }
