通过js或查询强制下载 [英] Force download through js or query

查看:116
本文介绍了通过js或查询强制下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过JS或Javascript强制下载,即网页不应该在浏览器的新选项卡中打开文件,而是弹出让用户选择另存为或打开???

Is it possible to force a download through JS or Javascript i-e the web page should not open the file in new tab in the browser but to pop up to let the user to choose eith "save as" or open with ???

推荐答案

您无法强制执行JavaScript中的行为,需要在服务器端设置HTTP标头:

You can not force that behavior from JavaScript, the HTTP Headers need to be set on the server side:

Content-disposition=attachment; filename=some.file.name

解决问题的方法是让你的AJAX方法重定向用户到PDF的URL:

The way you can solve the problem is to let your AJAX method redirect the user to the URL of the PDF:

location.replace('path/to.pdf');

(必须为PDF设置上述HTTP标头)

(The above HTTP headers must be set for the PDF)

更新

在回答时,它不是'可能。现在是,向下滚动以查看其他答案如此说明

At the time of this answer, it wasn't possible. Now it is, scroll down to see the other answer saying so.

这篇关于通过js或查询强制下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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