網站首頁 編程語言 正文
有時候前端需要轉化后端接口返回,轉化為自己想要的東西,例如后端返回表名并不是中文,我們需要自己解碼
-
這行代碼的作用是從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
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-12-13 bat文件與Vbs文件之間的常用操作(獲取用戶輸入,執行VBS文件)_DOS/BAT
- 2022-07-14 Qt創建SQlite數據庫的示例代碼_C 語言
- 2022-10-02 pandas數據類型之Series的具體使用_python
- 2021-12-07 基于Kubernetes實現前后端應用的金絲雀發布(兩種方案)_C#教程
- 2022-08-20 React的生命周期詳解_React
- 2022-04-25 C#使用NPOI設置Excel下拉選項_C#教程
- 2023-05-29 tf.nn.conv2d與tf.layers.conv2d的區別及說明_python
- 2022-03-22 Android中ListView使用示例介紹_Android
- 欄目分類
-
- 最近更新
-
- 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同步修改后的遠程分支