快速呈现渲染视图并同时下载文件 [英] Express render view and download file at the same time

查看:46
本文介绍了快速呈现渲染视图并同时下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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>

使用Javascript/jQuery下载文件

这篇关于快速呈现渲染视图并同时下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆