[英]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.