简体   繁体   English

从一个链接下载多个文件

[英]Download multiple files from one link

I would like to open multiple download dialog boxes after the user clicks on a link. 我想在用户点击链接后打开多个下载对话框。

Essentially what I am trying to do is allow the user to download multiple files. 基本上我要做的是允许用户下载多个文件。 I don't want to zip up the files and deliver one zipped file because that would require a lot of server resources given that some of the files are some what large. 我不想压缩文件并提供一个压缩文件,因为这需要大量的服务器资源,因为某些文件有些大。

My guess is that there may be some way with javascript to kick off multiple requests when the user clicks on a certain link. 我的猜测是,当用户点击某个链接时,javascript可能会有某种方式启动多个请求。 Or maybe there might be a way on the server side to start off another request. 或者也许服务器端可能有一种方法可以启动另一个请求。

Unless the client is configured to automatically download files, you can't accomplish this without packaging the files in a single response (like ZIP solution you mentioned.) This would be a security issue if a Web site would be able to put arbitrarily large number of files on your disk without telling you. 除非客户端配置为自动下载文件,否则如果不将文件打包在单个响应中(如您提到的ZIP解决方案),则无法实现此目的。如果网站能够放置任意大的数字,这将是一个安全问题磁盘上的文件没有告诉你。

By the way, you might be overestimating the cost of packaging in a single file. 顺便说一句,您可能会高估单个文件中的打包成本。 Streaming files is usually an I/O-bound operation. 流文件通常是一个I / O绑定操作。 There should be enough CPU cycles to spare for piping the data through some storage(tar)/compression(zip) methods. 应该有足够的CPU周期来通过一些存储(tar)/压缩(zip)方法来管理数据。

如果你绝对,肯定不能在服务器级压缩,这可能是创建某种自定义“下载管理器”客户端插件的好例子,你可以让用户安装,然后你可以完全控制多少您下载的文件,他们去过的地方等。

I suppose you could link to a frameset document or a document containing iframes. 我想你可以链接到框架集文档或包含iframe的文档。 Set the src of each from to one of the files you want to download. 将每个的src设置为您要下载的文件之一。

That said, a zipped version would be better. 也就是说,拉链版本会更好。 If you are concerned about the load then either: 如果你担心负载,那么:

  • zip the files with compression set to none 将压缩设置为none的文件压缩
  • use caching on the server so you zip each group of files only once 在服务器上使用缓存,因此您只需将每组文件压缩一次

I don't see what the big deal is with this. 我不明白这有什么大不了的。 Why not something like this: 为什么不是这样的:

<a href="javascript:" id="myLink">Click me</a>

<script type="text/javascript">
$('a#myLink').click(function() {
    window.open('http://www.mysite.com/file1.pdf', 'file1');
    window.open('http://www.mysite.com/file2.pdf', 'file2');
    window.open('http://www.mysite.com/file3.pdf', 'file3');
});
</script>
  1. Present a page with a form of check boxes of the available files for download - with multiple select enabled for the check boxes. 显示一个页面,其中包含可供下载的可用文件的复选框形式 - 复选框启用了多个选择。
  2. User selects multiple files and submits forms. 用户选择多个文件并提交表单。
  3. Server accepts request and creates a page with serial-triggered file download javascript. 服务器接受请求并创建一个带有串行触发文件下载javascript的页面。
  4. The page with the embedded javascript is presented to the user's browser, listing and asking for confirmation the files to be serially downloaded. 具有嵌入式javascript的页面被呈现给用户的浏览器,列出并要求确认要串行下载的文件。
  5. User clicks [yes - serially swamp my harddisk with these files] button. 用户点击[是 - 用这些文件连续淹没我的硬盘]按钮。
  6. foreach file, listener for download completed triggers the next download, until end of list. foreach文件,下载完成的监听器触发下一次下载,直到列表结束。

I only know how to do this using Google GWT, where I had set up GWT RPC between browser and server. 我只知道如何使用Google GWT做到这一点,我在浏览器和服务器之间设置了GWT RPC。 Took me two weeks to understand GWT RPC and perfect the download. 花了两周的时间来了解GWT RPC并完善了下载。 Now it seems rather simple. 现在看起来很简单。

Basically (do you know basically is one of the most used non-technical words among the geek community?), you have to declare a server service class specifying the datatype/class of transfer. 基本上(你知道基本上是geek社区中最常用的非技术词之一吗?),你必须声明一个服务器服务类,指定转移的数据类型/类。 Where the datatype must implement serializable. 数据类型必须实现可序列化的位置。 Then on the browser-side the GWT client declares a corresponding receiver class specifying the same serializable datatype. 然后在浏览器端,GWT客户端声明一个相应的接收器类,指定相同的可序列化数据类型。 The browser side implements a listener for onSuccess and onFailure. 浏览器端为onSuccess和onFailure实现一个监听器。

Hey, I even managed to augment GWT service base class so that I could use JSP rather than plain servlets to implement the service interface. 嘿,我甚至设法扩充GWT服务基类,以便我可以使用JSP而不是普通的servlet来实现服务接口。

Actually, I was not downloading a series of files but streams that conditionally serially triggered the next stream, because my onSuccess routine would inspect the current stream to decide what content to request for on the next stream. 实际上,我没有下载一系列文件,而是有条件地串行触发下一个流的流,因为我的onSuccess例程将检查当前流以决定在下一个流上请求什么内容。

Ok, two weeks was an exageration, it took me a week to do it. 好吧,两个星期是一个夸张,我花了一个星期的时间来做这件事。 A genius would have taken half a day only. 一个天才只需要半天。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM