html - 在 downloadLink 中下载 blob
问题描述
我想在 downloadLink 中下载应用程序/pdf。可能吗?将文件下载为pdf的最简单方法是什么?我有base64string。
const binaryString = window.atob(this.base64ToDisplay);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
const blob = new Blob([bytes], { type: 'application/pdf' });
const fileName = 'example.pdf';
const downloadLink = document.createElement('a');
downloadLink.href = blob;
downloadLink.download = fileName;
downloadLink.click();
如何下载这个文件?
解决方案
尝试这个:
private downloadFile(data: any, type: string) {
// create blob and build an URL
let blob = new Blob([data], { type: type });
let url = window.URL.createObjectURL(blob);
let downloadLink = document.createElement('a');
downloadLink.href = url;
// append url to element and trigger click
downloadLink.setAttribute('download', PDF_FILE_NAME);
document.body.appendChild(downloadLink);
downloadLink.click();
// clean up
document.body.removeChild(downloadLink);
}
推荐阅读
- c++ - C++ 中 Round 函数的未知行为
- python - 如何在 matplotlib.pyplot 填充函数后识别其颜色的某些坐标(x,y)?
- unit-testing - 子文件夹中的 Go Gin Gonic 单元测试
- c - C 中的标识符到底是什么?
- react-native - 如果使用 Hooks 重新渲染父级,则防止子级重新渲染
- c++ - c++ 可变参数连接路径
- ios - Swift tableView.reloadData() 在完成处理程序中不起作用
- java - 不兼容的类型。找到:'void',必需:'int',但有什么问题?
- grammar - 如何在 EBNF 中使用语句实现“返回”数字?
- c++ - 在 Visual Studio 2019 中控制 C++ 标准修订版