是否可以触发文件下载到用户的浏览器? [英] Is it possible to trigger file download to a user's browser?

查看:85
本文介绍了是否可以触发文件下载到用户的浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以强制将文件下载到用户的浏览器(当然会带有标准对话框提示)?由服务器触发。基本上,我正在考虑将文件从服务器文件系统推送到浏览器。还是由于安全沙箱而无法实现?
如果这不可能,那么有没有办法很接近这个?我不想发送未经用户同意的文件。我可以想到两步方法,首先提示用户输入数据,然后单击确定按钮触发下载,但是在下载即将结束时,用户将获得另一个确认框(要求打开的标准下载提示或保存)。

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). It would be nice if its possible with one step.

推荐答案

使用ajax json脚本,该脚本每5秒或10轮询一次服务器就很好了。秒,当服务器准备好文件时,
会给出一个肯定的回答我有文件,然后将iframe移至该文件所在的网址,然后将其作为普通下载进行处理。

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.

我提供了一个小例子,说明我认为它应该工作,因此您可以从中进行一些工作。我还没有检查错误,它更是概念证明。

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示例(不带json):

jQuery example(without 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代码

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

这篇关于是否可以触发文件下载到用户的浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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