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 表示服務器響應的數據類型,可以是 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 這個參數(代表:服務器允許瀏覽器訪問的頭(headers)的白名單),vue中就無法獲取 content-disposition,即 res.headers['content-disposition'];無法找到
總結
以上所述是小編給大家給大家介紹的vue element中axios下載文件(后端Python),希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
相關文章
Vue3中watch監(jiān)聽對象的屬性值(監(jiān)聽源必須是一個getter函數)
這篇文章主要介紹了Vue3中watch監(jiān)聽對象的屬性值,監(jiān)聽源必須是一個getter函數,本文結合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
vue3中使用ant-design-vue的layout組件實現動態(tài)導航欄和面包屑功能
這篇文章主要介紹了vue3中使用ant-design-vue的layout組件實現動態(tài)導航欄和面包屑功能,基于一個新建的Vue3項目上實現,本文結合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-01-01

