微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。