如何强制“另存为”?对话? [英] How can I force a "Save as" dialog?

查看:82
本文介绍了如何强制“另存为”?对话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法强制另存为对话框?

Is there a way to force a "Save As" dialog?

目前,如果点击该链接,您将在浏览器中看到该内容,但我需要出现另存为对话框。该文件位于外部服务器上。

Currently if you click on the link you will see the content in the browser, but I need the "Save As" dialog to appear. The file is located on a external server.

我已经尝试过:

$("#download_link").click(function() {
    document.execCommand('SaveAs','1','location');
});

不幸的是,这不起作用。

Unfortunately that does not work.

编辑:

当然,如果用户在浏览器中激活了此选项,则可以下载该文件。我只是不希望他被重定向到网站。

Of course it would be fine if the file just gets downloaded if the user has this option activated in his browser. I just do not want him to get redirected to the site.

如果有办法用PHP做这个也没关系,但它看起来像我需要使用readfile(),由于流量我不想要它。

It would be also okay if there is a way to do this with PHP, but it looks like that I need to do it with readfile(), which I do not want due to the traffic.

我需要一个适用于外部服务器上文件的解决方案!

评论,指出一些事情:


很抱歉我需要问你,但你是认真的吗?我刚问
我怎么能强制保存为对话框,你想出非法,
混淆等?我多次提到过,为什么我想这样做的唯一原因是为了避免所需文件直接在浏览器中播放
。这不是非法的,不是我要隐藏的东西。我也不知道为什么这个问题被标记为
作为主题。

Sorry that i need to ask you that, but are you serious? I just asked how i can force a save as dialog, and you come up with "illegal", "obfuscation" etc? I mentioned it multiple times that the only reason why i want to do that is to avoid that the desired file get's played directly in the browser. This is nothing illegal, not either something what i want to hide. I have also no clue why this question gets marked as off topic.


推荐答案

如果文件在浏览器中显示为文本或作为文件下载,则取决于随文件一起发送的标题。
例如,这是excelfile的标题:

If the file is displayed in the browser as text or get downloaded as a file depends on the header that get send with the file. For example this is the header for a excelfile:

 header("Content-Type: application/vnd.ms-excel");
 header("Content-Disposition: attachment; filename=\"myfilename.xlsx\"");
 header("Cache-Control: max-age=0");

(Excelfile是用php生成的,没有这个标题我会在浏览器中看到漏洞文件文本)

(the Excelfile is generated with php, without this header I would see the hole file in the browser as text)

重要的是Content-Type。

The important thing is the Content-Type.

正如你在问题中所说,你不想使用PHP的原因readfile()你也可以用htaccess设置标题。

As you say in your question you don't want to use PHP cause of readfile() you can also set the header with htaccess.

<FilesMatch "\.(?i:txt)$">
 ForceType application/octet-stream
</FilesMatch>

application / octet-stream =二进制文件(因此它总是下载)。
您必须重写FilesMatch以匹配您的文件

application/octet-stream = something binary (so it get allways downlaoded). You will have to rewrite the FilesMatch to match your files

这篇关于如何强制“另存为”?对话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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