日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

解碼方法decodedURIComponent

作者:Ybittersweet 更新時間: 2023-07-24 編程語言

有時候前端需要轉化后端接口返回,轉化為自己想要的東西,例如后端返回表名并不是中文,我們需要自己解碼

  • 這行代碼的作用是從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

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新