javascript - 如何将download.pdf重命名为react js中的任何自定义名称
问题描述
我收到base64格式的pdf。当我使用 window.open() pdf 下载打开它时,但 pdf 的名称是 download.pdf。我想自定义名称该 pdf 文件。提前致谢。
fetch(service_url, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
}).then(response => response.blob())
.then(response => {
var blob=response
var reader = new window.FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
var base64data = reader.result;
window.open(base64data);
}
})
.catch(error => {
console.error(error);
});
解决方案
我确实在一个项目中创建了一个实用程序,希望这会有所帮助。
exportToJson = el => {
if (!isEmpty(this.formData)) {
const obj = encodeURIComponent(JSON.stringify(this.formData, null, '\t'));
const data = "text/json; charset=utf-8," + "obj";
const date = new Date();
const fileName = "customName.json";
el.target.setAttribute('href', 'data:' + data);
el.target.setAttribute('download', fileName);
}
};
推荐阅读
- javascript - 如何将新版本的jquery代码更改为旧版本的代码
- c# - 将应用程序服务与 IFormFile 解耦
- google-chrome-devtools - 如何恢复 RSA 指纹对话框?
- vue.js - 在多个 Nuxt 应用程序之间共享 Vuex 状态
- c# - 何时在 MVC 中加载自定义 CSS
- python - Minio minio.error.SignatureDoesNotMatch 抛出 try-except
- excel - 从当前打开的从 Sharepoint 下载的 Word 文档 .docm 中获取字节数组,而不保存到本地驱动器
- opencv - 如何使用opencv java对缺少角的图像进行透视变换
- wordpress - 如何在没有插件的 wordpress 代码编辑器中用 TinyMCE 替换 CodeMirror?
- node.js - 在 react js 项目中安装纱线时出错