download - 在客户端 Vue App 下载 CSV 文件的正确方法
问题描述
我有一个 Vue 应用程序,其中有一个“downloadCsv”方法。当按下按钮时,将调用此方法并进行以下 GET 调用:
@GetMapping(value = "downloadCsv")
public ResponseEntity<Resource> exportSubmitted() throws IOException {
//Generates CSV file and returns fileName
String filePath = myService.generateCSV();
InputStreamResource resource = new InputStreamResource(new FileInputStream(filePath));
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + "data.csv" + "\"")
.body(resource);
}
当我直接调用这个端点时,即localhost:8080/downloadCsv
在浏览器(chrome)中,csv 被下载。
但是,当我在点击时从我的 Vue 应用程序中调用此端点时,即:
axios.get(endpoint)
.then(result => {
const blob = new Blob([result], {type: 'text/csv'});
FileSaver.saveAs(blob, 'download.csv');
});
我无法让浏览器下载文件。
在Vue中调用返回文件并在浏览器中下载文件的rest端点的正确方法是什么?
解决方案
推荐阅读
- sql - 更新查询给出奇怪的错误“CfPlnUt_Aemk5Mr77-AevA2”
- pagination - Gremlin 中的分页
- python - 这个错误 TypeError: 'Button' object is not callable 是什么意思?
- python - 如果列不同于 NaN 或 0,则 np.any() 替代
- sql - sql中的匹配元组
- jdbc - Logstash JDBC 适配器:Varbinary 到 UTF-8?(mysql到弹性导入)
- python - SKlearn对形状与训练数据集形状不同的测试数据集进行预测
- angular - 在茉莉花单元测试中使用具有参数的函数单击按钮(角度)
- apache - 如何在 Apache 反向代理上设置连接属性
- sql-server-2017 - 标准版 Linux 上 SQL Server 2017 的透明数据库加密