使用Express下载文件 [英] Downloading file with Express

查看:71
本文介绍了使用Express下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Express,我需要从服务器下载文件.我可以通过< a href ="mylink"></a> 轻松下载它,但是有一些查询参数,我想向用户隐藏.

I'm using Express and I need to download a file from server. I can easily download it with just <a href="mylink"></a>, but there is some query parameters, that I want to hide from user.

因此,现在我尝试使用jQuery.ajax,该请求将请求发送到Express(srv1),然后将具有我的参数的Express请求发送到另一个服务器(srv2).服务器用"Content-disposition"标头和文件数据响应我.没关系.还有一个问题-我可以使用该文件并对我的初始ajax请求进行响应吗?

So now I'm trying to use jQuery.ajax that sending a request to Express (srv1) and then Express sending request with my parameters to the another server (srv2). Server responds me with 'Content-disposition' header and a file data. It's ok. And there is a question - can I use that file and respond with it to my initial ajax request?

问题是,即使 res.download()带有文件(已经在我的srv1上)也无法正常工作.Express可以很好地设置标题,但是没有文件提示下载.也许ajax中有一个探针?

The problem is, that even res.download() with files (that already on my srv1) doesn't work. Express sets headers well, but no file is prompting to download. Maybe there is a probem in ajax?

推荐答案

您不能使浏览器通过javascript ajax请求执行文件下载(这是安全限制).有关详细信息,请参见 https://stackoverflow.com/a/9970672/266795 .您需要正常的浏览器GET或POST请求,才能获得正确的文件保存对话框.

You cannot cause the browser to perform a file download with a javascript ajax request (this is a security limitation). See https://stackoverflow.com/a/9970672/266795 for details. You'll need a normal browser GET or POST request to get a proper file save dialog.

这篇关于使用Express下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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