我一直在寻找一个前端 js 解决方案来解决这个问题/在浏览器中强制下载文件的概念。基本上,我想让浏览器通过一些 js 事件下载文件。我知道在 HTML5 中我们有下载属性,但它仅在 Chrome 中受支持,最终用户仍然必须实际单击链接,但我无法触发它。但缺乏浏览器支持对于我的需求来说更成问题。
所以,我想我会使用 PHP 和“Content-Disposition:附件”,它工作得很好,但现在它是服务器端代码,我想知道它是否会影响我的带宽。下载的文件是外部的,不在我的服务器上。
另外,虽然“download.php?file=http://domain.com/image.jpg”为我下载文件,但我不知道如何通过对“download.php?file”的ajax请求启动下载=http://domain.com/image.jpg"
有什么想法吗?
这是 php 代码的一部分:
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
readfile("$filename");
好的,在前端启动下载的一种方法是将 X-Frame-Options 标头应用到 download.php,然后在 iframe 中打开图像,如下所示to download.php:
header('X-Frame-Options: DENY');
然后用 JS/jquery 执行此操作:
$("body").append(<iframe src="download.php?file=http://domain.com/image.jpg"></iframe>);
但我仍在使用 download.php 并且会产生带宽成本,所以它并不理想。还有其他想法吗?我只想在页面上显示一堆图像并允许用户单击一个按钮将它们全部下载。图像不是由我托管的,我不想占用带宽。
就下载 HTML5 属性而言,无法触发它,jquery 单击或触发单击失败,但此代码确实允许我以编程方式触发下载:
var clickEvent = document.createEvent("MouseEvent");
clickEvent.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
$("a")[0].dispatchEvent(clickEvent);
但是,此下载属性仅在 Chrome 中受支持,所以我越来越接近但还没有到那里。有人吗?
I was looking for a front-end js solution to this problem/concept of forcing file downloads in the browser. Basically, I want to make the browser download a file via some js event. I know in HTML5 we have the download property but its only supported in Chrome and the end-user still has to actually click on the link and I can't trigger it. But the lack of browser support is more problematic for my needs.
So, I thought that I would use PHP and "Content-Disposition: attachment" which is working great but now that it's server side code I wonder if it will affect my bandwidth. The files downloaded are external and do not live on my servers.
Also, while "download.php?file=http://domain.com/image.jpg" downloads the file for me, I can't figure out how to initiate a download via an ajax request to "download.php?file=http://domain.com/image.jpg"
Any ideas?
Here is a part of the php code:
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
readfile("$filename");
Ok, well one way to initiate a download on the front-end is to apply a X-Frame-Options header to download.php and then open the image in an iframe, as in add this to download.php:
header('X-Frame-Options: DENY');
And then do this with JS/jquery:
$("body").append(<iframe src="download.php?file=http://domain.com/image.jpg"></iframe>);
But I am still using download.php and will get a bandwidth cost so it's not ideal. Any other ideas? I just want to display a bunch of images on the page and allow the user to click one button to download them all. Images are not hosted by me and I don't want to take the bandwidth hit.
In terms of the download HTML5 attribute and not being able to trigger it, the jquery click or trigger click fails but this code did allow me to trigger the download programmatically:
var clickEvent = document.createEvent("MouseEvent");
clickEvent.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
$("a")[0].dispatchEvent(clickEvent);
But, this download attribute is only supported in Chrome, so I am getting closer but not there yet. Anyone?
发布评论
评论(2)
看起来您正在使用 PHP 来获取远程文件,然后发出请求的 Web 浏览器将从您的 PHP 服务器下载。是的,它必须将原始源的全部内容复制到服务器,并将复制的内容发送到浏览器,从而影响带宽。
本质上,您拥有一个基本代理,如果您不过滤下载请求的链接,发送大文件请求的人可能会滥用该代理来耗尽您的数据传输限制。
另外,我不明白为什么您想要从 AJAX 请求开始下载,即使这是可能的。要下载的文件不应影响加载的页面。
It looks like you are using PHP to fetch the remote file, and then the requesting web browser would be downloading from your PHP server. Yes, it is affecting bandwidth by having to copy the entire contents of the original source to your server, and sending that copied content to the browser.
Essentially, you have a basic proxy that may be abused by people sending requests for large files to exhaust your data transfer limit if you do not filter the link to the download request.
Also, I don't see why you would want to start a download from an AJAX request, even if it were possible. The file to download should not affect the page that is loaded.
如果我转到某个页面并开始自动下载文件,我会单击“取消”并尝试关闭浏览器。我怀疑很多其他人也会有同样的观点,希望浏览器制造商也有同样的观点。如果系统提示我“您想下载此文件吗”,我希望如此。
要回答您的问题,如果文件是从服务器 B 下载的,那么服务器 B 上的带宽将受到影响。
If I go to a page and it starts an automatic download of a file, I click cancel and try and close my browser down. I suspect a lot of other people would be of the same opinion and hopefully manufacturers of browsers. I'd like it if I was prompted 'would you like to download this file'.
To answer your question if the files are being downloaded from Server B then it would be the bandwidth on Server B that would be affected.