上传前分割文件?
在网页上,是否可以在文件上传到服务器之前将大文件分割成块?例如,将 10MB 文件分割成 1MB 块,一次上传一个块,同时显示进度条?
听起来 JavaScript 没有任何文件操作能力,但是 Flash 和 Java 小程序呢?
这需要在 IE6+、Firefox 和 Chrome 中运行。 更新:忘记提及 (a) 我们正在使用 Grails,并且 (b) 这需要通过 https 运行。
On a webpage, is it possible to split large files into chunks before the file is uploaded to the server? For example, split a 10MB file into 1MB chunks, and upload one chunk at a time while showing a progress bar?
It sounds like JavaScript doesn't have any file manipulation abilities, but what about Flash and Java applets?
This would need to work in IE6+, Firefox and Chrome. Update: forgot to mention that (a) we are using Grails and (b) this needs to run over https.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试Plupload。它可以配置为检查用户端可用的任何运行时,无论是 Flash、Silverlight、HTML5、Gears 等,然后首先使用满足所需功能的那个。除此之外,它还支持图像大小调整(在用户端,保留 EXIF 数据(!))、流式传输和分段上传以及分块。文件可以在用户端分块,并逐块发送到服务器端处理程序(需要在服务器上进行一些额外的处理),以便大文件可以上传到最大文件大小限制设置为低得多的值的服务器例如,然后是它们的大小。还有更多。
我相信有些运行时支持 https,有些需要测试。不管怎样,现在那里的开发人员反应非常积极。所以你至少可以尝试一下;)
You can try Plupload. It can be configured to check whatever runtime is available on users side, be it - Flash, Silverlight, HTML5, Gears, etc, and use whichever satisfies required features first. Among other things it supports image resizing (on users side, preserving EXIF data(!)), stream and multipart upload, and chunking. Files can be chunked on users side, and sent to a server-side handler chunk-by-chunk (requires some additional care on server), so that big files can be uploaded to a server having max filesize limit set to a value much lower then their size, for example. And more.
Some runtimes support https I believe, some need testing. Anyway, developers on there are quite responsive these days. So you might at least try ;)
据我所知,唯一允许这样做的选择是签名的 Java 小程序。
未签名的小程序和 Flash 影片没有文件系统访问权限,因此它们无法读取文件数据。 Flash 能够上传文件,但其中大部分是由内置 Flash 实现处理的,据我所知,文件内容永远不会暴露给您的代码。
The only option I know of that would allow this would be a signed Java applet.
Unsigned applets and Flash movies have no filesystem access, so they wouldn't be able to read the file data. Flash is able to upload files, but most of that is handled by the built-in Flash implementation and from what I remember the file contents would never be exposed to your code.
没有适用于该选择的浏览器的 JavaScript 解决方案。有 File API,但虽然它可以在较新的 Firefox 和 Chrome 版本中工作,但它不会发生在 IE 中(IE9 beta 中也没有迹象)。
无论如何,在本地读取文件并通过 XMLHttpRequest 上传的效率很低,因为 XMLHttpRequest 不具备发送纯二进制的能力,只能发送 Unicode 文本。您可以使用 base-64 将二进制编码为文本(或者,如果您真的很专注,可以使用您自己的自定义 7 位编码),但这比普通文件上传的效率要低。
您当然可以使用 Flash 进行上传(请参阅 SWFUpload 等),如果您必须的话,甚至可以使用 Java (Jumploader... 不过,这些天我不会打扰,因为 Flash 流行率非常高,而且 Java 插件持续下降)。您不一定会获得低级控制来分割成块,但您真的需要吗?做什么的?
另一种可能的方法是使用标准 HTML 文件上传字段,并在提交发生时设置一个间隔调用,以使用 XMLHttpRequest 轮询服务器,询问文件上传进行了多远。这需要在服务器端进行一些工作,将当前上传进度存储在会话或数据库中,以便另一个请求可以读取它。它还意味着使用可以为您提供进度回调的表单解析库,而大多数标准语言内置语言(例如 PHP)则没有。
无论您做什么,都应采用“渐进增强”方法,允许不支持的浏览器回退到纯 HTML 上传。浏览器通常有一个用于 HTML 文件上传的上传进度条,但它往往很小并且很容易错过。
There is no JavaScript solution for that selection of browsers. There is the File API but whilst it works in newer Firefox and Chrome versions it's not going to happen in IE (no sign of it in IE9 betas yet either).
In any case, reading the file locally and uploading it via XMLHttpRequest is inefficient because XMLHttpRequest does not have the ability to send pure binary, only Unicode text. You can encode binary into text using base-64 (or, if you are really dedicated, a custom 7-bit encoding of your own) but this will be less efficient than a normal file upload.
You can certainly do uploads with Flash (see SWFUpload et al), or even Java if you must (Jumploader... I wouldn't bother, these days, though, as Flash prevalence is very high and the Java plugin continues to decline). You won't necessarily get the low-level control to split into chunks, but do you really need that? What for?
Another possible approach is to use a standard HTML file upload field, and when submit occurs set an interval call to poll the server with XMLHttpRequest, asking it how far the file upload is coming along. This requires a bit of work on the server end to store the current upload progress in the session or database, so another request can read it. It also means using a form parsing library that gives you progress callback, which most standard language built-in ones like PHP's don't.
Whatever you do, take a ‘progressive enhancement’ approach, allowing browsers with no support to fall back to a plain HTML upload. Browsers do typically have an upload progress bar for HTML file uploads, it just tends to be small and easily missed.
您是否特别需要将其分成 X 块?或者您正在尝试解决上传大文件引起的问题? (例如,无法在客户端重新启动上传,当整个文件一次上传并保存在内存中时,服务器端崩溃)
搜索流式上传组件。这取决于您所使用的技术以及您更喜欢哪个组件 jsp、asp.net 等。
http: //krystalware.com/Products/SlickUpload/ 这是一个服务器端产品
这里有一些指向各种上传器的更多指针 http://weblogs.asp.net/jgalloway/archive/2008/01/08/large-file-uploads-in-asp-net.aspx
有些尝试管理服务器上的内存,例如因此整个大文件不会同时存在于内存中,有些人尝试管理客户端体验。
Do you specifically need it two be in X chunks? Or are you trying to solve the problems cause by uploading large files? (e.g. can't restart an upload on the client side, server side crashes when the entire file is uploaded and held in memory all at once)
Search for streaming upload components. It depends on what technologies you are working with as to which component you will prefer jsp, asp.net, etc.
http://krystalware.com/Products/SlickUpload/ This one is a server side product
Here are some more pointers to various uploaders http://weblogs.asp.net/jgalloway/archive/2008/01/08/large-file-uploads-in-asp-net.aspx
some try to manage memory on the server,e.g. so the entire huge file isn´t in memory at one time, some try to manage the client side experience.