vue element中axios下載文件(后端Python)
•axios 接受文件流,需要設置 {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 表示服務器響應的數(shù)據(jù)類型,可以是 arraybuffer , blob, document , json , text , stream . 默認為: 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 這個參數(shù)(代表:服務器允許瀏覽器訪問的頭(headers)的白名單),vue中就無法獲取 content-disposition,即 res.headers['content-disposition'];無法找到
總結(jié)
以上所述是小編給大家給大家介紹的vue element中axios下載文件(后端Python),希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
相關文章
iview-table組件嵌套input?select數(shù)據(jù)無法雙向綁定解決
這篇文章主要為大家介紹了iview-table組件嵌套input?select數(shù)據(jù)無法雙向綁定解決示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09vue-pdf實現(xiàn)pdf在線預覽并實現(xiàn)自定義預覽框高度
這篇文章主要介紹了vue-pdf實現(xiàn)pdf在線預覽并實現(xiàn)自定義預覽框高度方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03