【发布时间】:2022-09-24 21:01:44
【问题描述】:
我正在从API 读取数据并下载file。我想在react 应用程序的public 文件夹中保存相同的file。
this.state = {
fileDownloadUrl: null,
fileName: \'\',
};
下面是方法。
downLoadFile(e) {
e.preventDefault();
axios
.get(
\'http://localhost:8080/file_download/en\'
)
.then((res) => {
var output = res.data;
const blob = new Blob([output]);
const fileDownloadUrl = URL.createObjectURL(blob);
this.setState({ fileDownloadUrl: fileDownloadUrl }, () => {
this.dofileDownload.click();
URL.revokeObjectURL(fileDownloadUrl);
this.setState({ fileDownloadUrl: \'\' });
});
});
}
下载文件的代码。
<a
className=\"hidden\"
download={this.state.fileName + \'.json\'}
href={this.state.fileDownloadUrl}
ref={(e) => (this.dofileDownload = e)}
>
download it
</a>
无论如何我可以修改code 以便file 可以保存在react 应用程序的public 文件夹中。稍后此文件将用于翻译。
标签: javascript reactjs react-native react-redux react-hooks