Javascript:Chrome 浏览器在文件下载中添加尾部斜杠

问题描述

我在浏览器中处理的文件下载实现中遇到了一个奇怪的问题

//get the response from api 
const result: any = await this.getClaimApiService.downloadDocumentAPICall(
  params
);

const contentdisposition =
  result.headers.get('content-disposition') || '';
const matches = /filename=([^;]+)/gi.exec(contentdisposition);
let fileName = 'file-not-found';
if (Array.isArray(matches)) {
  fileName = (matches[1] || 'untitled').trim();
}

const a = document.createElement('a');
document.body.appendChild(a);
const blob: any = new Blob([result.body],{ type: 'octet/stream' });
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);

如上所示,文件名是从 content-disposition 标头中提取的,并根据该名称下载文件

但如果文件名包含空格,例如 sample doc.pdf 那么下载的文件将在文件末尾添加一个斜杠,如 sample doc.pdf_,这会使文件不损坏

我不知道如何解决这个问题,因为我无法控制后端

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)