问题描述
我想使用express渲染文件并同时下载文件
I would like to use express to render a file AND download a file at the same time
我当前的代码如下:
res.attachment('filename.csv');
res.render('pages/result', { data });
但是,如果我这样做,它只会下载数据而不会呈现视图
However, if I do this, it only downloads the data and does not render the view
我想要呈现一个成功页面,然后发送文件以便下载
What I want is to render a success page, and then send the file so that it downloads
我需要使用1个端点来完成此操作,因为我需要生成文件,并且只有在文件成功的情况下,我才会呈现成功页面
I need this to be done with 1 endpoint, because, I need to generate the file and only if it is successful, I would render the success page
我可以使用1个端点执行此操作吗?
Am I able to do this with 1 endpoint?
谢谢
推荐答案
将文件的路径作为变量发送到渲染页面,并在javascript中编写一个函数以自动下载文件.
send the path of the file to the render page as variable and write a function in javascript to download the file automatically.
res.render('pages/result', { path: '../folder/filename.csv'});
渲染页面上
<iframe id="my_iframe" style="display:none;"></iframe>
<script>
function Download(url) {
document.getElementById('my_iframe').src = #{path};
};
</script>
这篇关于快速呈现渲染视图并同时下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!