新聞中心
這篇文章給大家分享的是有關(guān)vue element中axios如何下載文件的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
成都創(chuàng)新互聯(lián)是專業(yè)的善左網(wǎng)站建設(shè)公司,善左接單;提供成都網(wǎng)站建設(shè)、成都網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行善左網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
?axios 接受文件流,需要設(shè)置 {responseType:'arraybuffer'}
axios.post( apiUrl, formdata, {responseType:'arraybuffer'} ).then(res=> { if (res.status === 200) { let blob = new Blob([res.data], { type: res.headers['content-type'] }); const fileName = res.headers['content-disposition']; const title = fileName && (fileName.indexOf('filename=') !== -1) ? fileName.split('=')[1] : 'download'; require('script-loader!file-saver'); saveAs(blob, title); } }) .catch();
注: axios 中 response 表示服務(wù)器響應(yīng)的數(shù)據(jù)類型,可以是 arraybuffer , blob, document , json , text , stream . 默認(rèn)為: json
?后端發(fā)送文件:Python
from flask import send_from_directory @admin_bp.route('/tasksothers/download', methods=["GET", "POST"]) @auth.login_required def api_tasksothers_download(): root_path = '' src_name = "a.sql" upload_path = os.path.join(root_path, src_name) print("upload_path =", upload_path) if os.path.isfile(upload_path): response = send_from_directory(root_path, src_name, as_attachment=True) print("response: ",response) response.headers["Access-Control-Expose-Headers"] = "Content-disposition" print("response: ", response.headers) return response
注: 如果 response.header 中沒有添加 Access-Control-Expose-Headers 這個(gè)參數(shù)(代表:服務(wù)器允許瀏覽器訪問的頭(headers)的白名單),vue中就無法獲取 content-disposition,即 res.headers['content-disposition'];無法找到
感謝各位的閱讀!關(guān)于“vue element中axios如何下載文件”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
當(dāng)前名稱:vueelement中axios如何下載文件
網(wǎng)頁路徑:http://www.ef60e0e.cn/article/iijhhd.html