简体   繁体   English

是否可以触发文件下载到用户的浏览器?

[英]Is it possible to trigger file download to a user's browser?

Is it possible to force file download to user's browser (with the standard dialog prompt of course)? 是否可以强制文件下载到用户的浏览器(当然有标准的对话框提示)? To be triggered by the server. 由服务器触发。 Basically I'm thinking of pushing a file to the browser coming from server filesystem. 基本上我正在考虑将文件推送到来自服务器文件系统的浏览器。 Or is this not possible because of security sandbox? 或者由于安全沙箱,这是不可能的? If this is not possible, is there a different way very close to this? 如果这是不可能的,有不同的方式非常接近这个? I do not want to send the file w/o the user's consent. 我不想在用户同意的情况下发送文件。 I can think of two step approach, first to prompt user of incoming data, and let user click on an OK button which triggers download, but near the end of download, user will get another confirmation box (the standard download prompt which asks to open or save). 我可以想到两步法,首先提示用户输入数据,然后让用户点击OK按钮触发下载,但接近下载结束时,用户将获得另一个确认框(标准下载提示,要求打开)或保存)。 It would be nice if its possible with one step. 如果一步到位可能会很好。

It's definitely possible. 这绝对是可能的。 Assuming you want this to be triggered by the server, you would simply send them this header: 假设您希望服务器触发此操作,您只需向其发送此标头:

Location: http://www.foo.com/path/to/file

Where foo.com is your domain and it contains the path to the file. 其中foo.com是您的域,它包含文件的路径。 This would send the user over to your file link and have them auto-download as a result. 这会将用户发送到您的文件链接,并将其自动下载。

Now, to get around the issue where your browser views the content, you would need server-side code to issue Content header information like so (using PHP as an example): 现在,为了解决浏览器查看内容的问题,您需要服务器端代码来发布Content头信息(使用PHP作为示例):

<?php
    header("Content-Type: application/octet-stream");
    header("Content-Length: " . filesize($file));   
    header('Content-Disposition: attachment; filename=' . $file); 
    readfile($file);
?>

Hopefully this works well enough as pseudo-code to get you started. 希望这能够很好地作为伪代码来帮助您入门。 Good luck! 祝好运!

Use an ajax json script that polls the server every 5 seconds or 10 seconds, when the server has a file ready responds with a positive answer "I have a file" have an iframe move to that url where the file is, and then it's handled as a normal download. 使用ajax json脚本每隔5秒或10秒轮询一次服务器,当服务器准备好文件时,回答“我有一个文件”,iframe移动到该文件所在的url,然后处理作为正常下载。

I have provided a little example of how I should think that it should work, so you have something to work from. 我提供了一个小例子,说明我应该如何认为它应该有效,所以你有一些工作要做。 I haven't checked it for bugs though, it's more a proof of concept thing. 我没有检查它的错误,它更像是一个概念验证的东西。

jQuery example(without json): jQuery示例(不带json):

$.ajax('/myserverscript.php?fileready=needtoknow').done(function(data) 
    {
    if(data.indexOf("I have a file") != -1)
        {
        xdata = data.split('\n');
        data = xdata[1];
        document.getElementById('myiframe').location.href = data;
        }
    });

PHP code PHP代码

$x = filecheckingfunction();// returns false if none, returns url if there is a file
if($x !== false)
    {
    echo 'I have a file\n';
    echo $x;
    }

Since you are aware that you must prompt the user, try using a plain hyperlink with type="application/octet-stream" . 由于您知道必须提示用户,因此请尝试使用type="application/octet-stream"的普通超链接。

You can also use: "content-disposition: attachment" in the header. 您还可以在标题中使用: "content-disposition: attachment"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以知道文件是否在用户的浏览器缓存中? - Is it possible to know if a file is inside the user's browser cache? 是否可以使用键盘来触发JavaScript中的文件浏览器? - Is it possible to use the keyboard to trigger a File Browser in JavaScript? 使用JQuery触发浏览器下载 - Trigger a browser download with JQuery 在浏览器的本地存储中下载并保存文件 - Download and save file in browser's local storage 是否可以在浏览器中使用javascript对用户的系统进行基准测试 - Is it possible to benchmark a user's system with javascript in the browser 可以在javascript中生成文件并提示用户下载? - Possible to generate a file in javascript and prompt the user to download? 如果以文件名的附件形式接收到AngularJS $ resource响应,如何触发Spinner进行文件下载或如何在浏览器中下载文件 - How to trigger Spinner for file download or How to download file in browser if AngularJS $resource response received as an attachement with file name 是否可以在没有输入设备的情况下触发浏览器的事件处理程序? - Is it possible to trigger a browser's event handlers without input devices? 在浏览器外部下载Javascript触发器 - Javascript trigger download outside of browser 恢复从用户机器到 Azure Blob 存储的文件上传,并从浏览器以 Typescript(angular)将文件从 Azure Blob 下载到用户机器 - Resume File Upload from User's machine to Azure Blob Storage and Download file from Azure Blob to User's machine in Typescript(angular) from Browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM