網站首頁 編程語言 正文
有時候前端需要轉化后端接口返回,轉化為自己想要的東西,例如后端返回表名并不是中文,我們需要自己解碼
-
這行代碼的作用是從HTTP響應頭部字段
content-disposition
中提取出文件名,并將其賦值給變量filename。
const filename = decodeURIComponent(res.headers["content-disposition"].match(/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?/)[1]);
-
這段代碼的作用是將從服務器獲取到的文件數據
轉化為Blob對象,并生成一個URL地址
,然后將文件名和URL地址封裝在一個對象中
,最后將該對象返回。
let blob = new Blob([res.data], { type: 'application/msword;charset=utf-8' })
let url = window.URL.createObjectURL(blob)
const data={filename,url}
return data; // 將生成的 URL 地址返回
//下載word
export function FormDataDownloadReport(ID) {
return request({
url: `/api/TaskFormData/DownloadReport/${ID}`,
method: "post",
responseType: "arraybuffer" // 指定響應數據類型為二進制數組
})
.then(res => {
const filename = decodeURIComponent(res.headers["content-disposition"].match(/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?/)[1]);
let blob = new Blob([res.data], { type: 'application/msword;charset=utf-8' })
let url = window.URL.createObjectURL(blob)
const data={filename,url}
return data; // 將生成的 URL 地址返回
});
}
解碼方法decodedURIComponent
const encodedURIComponent = "測試%3D%E6%B5%8B%E8%AF%95%20%E9%A1%B5%E9%9D%A2";
const decodedURIComponent = decodeURIComponent(encodedURIComponent);
console.log(decodedURIComponent); // 輸出:"測試=測試 頁面"
原文鏈接:https://blog.csdn.net/Ybittersweet/article/details/131551126
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2023-03-26 詳解C/C++中低耦合代碼的設計實現_C 語言
- 2023-02-12 Python利用物理引擎Pymunk編寫一個解壓小游戲_python
- 2022-12-11 C語言如何計算兩個數的最小公倍數_C 語言
- 2023-01-03 go?doudou開發gRPC服務快速上手實現詳解_Golang
- 2022-06-19 微信小程序前端如何調用python后端的模型詳解_python
- 2023-03-27 Python?tkinter中label控件動態改變值問題_python
- 2022-10-25 Git中smart?Checkout與force?checkout的區別及說明_相關技巧
- 2022-10-06 C語言實現短字符串壓縮的三種方法詳解_C 語言
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支