javascript 上传前拆分文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4099929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:22:19  来源:igfitidea点击:

Splitting a file before upload?

javascriptflashfile-uploadapplet

提问by Yevgeniy Brikman

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?

在网页上,是否可以在文件上传到服务器之前将大文件拆分成块?例如,将一个 10MB 的文件拆分为 1MB 的块,并在显示进度条的同时一次上传一个块?

It sounds like JavaScript doesn't have any file manipulation abilities, but what about Flash and Java applets?

听起来 JavaScript 没有任何文件操作能力,但是 Flash 和 Java 小程序呢?

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.

这需要在 IE6+、Firefox 和 Chrome 中工作。更新:忘记提及 (a) 我们正在使用 Grails 和 (b) 这需要通过 https 运行。

采纳答案by jayarjo

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.

你可以试试Plupload。它可以配置为检查用户端可用的任何运行时,无论是 Flash、Silverlight、HTML5、Gears 等,并首先使用满足所需功能的任何一个。除其他外,它支持图像大小调整(在用户端,保留 EXIF 数据(!))、流和分段上传以及分块。文件可以在用户端分块,并逐块发送到服务器端处理程序(需要对服务器进行一些额外的照顾),以便大文件可以上传到最大文件大小限制设置为低得多的值的服务器然后它们的大小,例如。和更多。

Some runtimes support https I believe, some need testing. Anyway, developers on there are quite responsive these days. So you might at least try ;)

我相信有些运行时支持 https,有些需要测试。无论如何,这些天那里的开发人员非常敏感。所以你至少可以尝试;)

回答by Herms

The only option I know of that would allow this would be a signed Java applet.

我所知道的唯一允许这将是签名的 Java 小程序的选项。

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.

未签名的小程序和 Flash 电影没有文件系统访问权限,因此它们无法读取文件数据。Flash 能够上传文件,但其中大部分是由内置的 Flash 实现处理的,据我所知,文件内容永远不会暴露给您的代码。

回答by bobince

There is no JavaScript solution for that selection of browsers. There is the File APIbut 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).

没有针对该浏览器选择的 JavaScript 解决方案。有文件 API,但虽然它适用于较新的 Firefox 和 Chrome 版本,但它不会在 IE 中发生(在 IE9 beta 中也没有迹象)。

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.

在任何情况下,本地读取文件并通过 XMLHttpRequest 上传它的效率都很低,因为 XMLHttpRequest 不能发送纯二进制文件,只能发送 Unicode 文本。您可以使用 base-64 将二进制编码为文本(或者,如果您真的很专注,可以使用您自己的自定义 7 位编码),但这会比普通文件上传效率低。

You can certainly do uploads with Flash (see SWFUploadet 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?

您当然可以使用 Flash 上传(请参阅SWFUpload等),如果必须,甚至可以使用 Java(Jumploader...我不会打扰,但现在,因为 Flash 流行度非常高,而 Java 插件继续下降) . 您不一定需要将低级控件拆分为多个块,但您真的需要这样做吗?做什么的?

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.

另一种可能的方法是使用标准的 HTML 文件上传字段,当发生提交时,设置一个间隔调用以使用 XMLHttpRequest 轮询服务器,询问它文件上传进行了多远。这需要在服务器端做一些工作,将当前上传进度存储在会话或数据库中,以便另一个请求可以读取它。这也意味着使用一个表单解析库,它可以为您提供进度回调,而大多数标准语言内置的(如 PHP)则没有。

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.

无论您做什么,都要采取“渐进式增强”方法,让不支持的浏览器退回到纯 HTML 上传。浏览器通常有一个用于 HTML 文件上传的上传进度条,它往往很小而且很容易错过。

回答by MatthewMartin

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)

你是否特别需要它两个在 X 块中?或者您是否正在尝试解决上传大文件导致的问题?(例如,无法在客户端重新启动上传,当整个文件一次上传并保存在内存中时,服务器端崩溃)

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.

搜索流式上传组件。这取决于您使用的技术是您更喜欢哪个组件,如 jsp、asp.net 等。

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

http://krystalware.com/Products/SlickUpload/这是一个服务器端产品 这里有更多指向各种上传器的指针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.

一些尝试管理服务器上的内存,例如整个大文件一次不在内存中,一些尝试管理客户端体验。