如何通过ajax获取文件上传进度?
到目前为止,我已经弄清楚如何使用 Ajax 和 PHP 异步上传文件,没有问题。但我想获取已上传文件的百分比,因为它正在上传,并且经过几个小时的研究,我找不到一个好的方法来做到这一点没有作弊。
一些 实现 我见过使用 Flash 来上传,并且在 Flash 中获取百分比显然相当常见,但如果可以的话,我想避免这种情况。
有什么想法吗?
So far I've figured out HOW to upload files asynchronously with Ajax and PHP, no problem there. But I want to get the percentage of the file that's already been uploaded, as it's uploading, and, after hours of research, I can't find a good way to do this without cheating.
Some implementations I've seen used Flash to upload, and getting the percentage in Flash is apparently fairly common, but I'd like to avoid this if I can.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
核心问题是 RFC 1867,通过 < code>multipart/form-data MIME类型,不提供任何提供文件上传进度的方法。
文件上传实际上只是一种奇特的表单提交。 CGI 脚本、PHP 和所有其他依赖前端 Web 服务器首先接受请求的 Web 技术可能在整个上传完成之前可能不会真正开始执行。这意味着他们通常甚至不知道上传何时开始,只能知道上传何时完成。
PHP 的 APC 扩展的新版本包括 此问题的解决方法 执行某种级别的黑魔法,使其能够更早地了解上传情况。不过,它仅作为 mod_php 的一部分起作用。开发人员似乎没有计划在FastCGI下支持它 。
另一个服务器端选项是“uploadprogress”PECL 扩展。我不太确定它使用的是哪种黑魔法。 来源表明它实际上挂接到
multipart
MIME 部分的处理。 (这表明至少有一些 SAPI 在客户端上传数据时将表单数据流式传输到 PHP。我知道至少有一些 FastCGI 服务器在传递整个请求之前缓冲整个请求,所以这可能不适合您。YMMV。)选项用于正常文件上传。 Ajax——或者更确切地说,XMLHttpRequest——不支持文件上传操作。该领域的大多数解决方法都涉及创建 iframe 并在那里提交表单,这也意味着其他人的客户端工作。如果您要经历这种级别的跳圈,您也可以使用现代文件上传小部件之一。
就我个人而言,我使用 Plupload,这是一个 Javascript 小部件,可以与每个人最喜欢的 Javascript 库 jQuery 配合使用。其他一些人则非常信赖 Uploadify。无论如何,这两个小部件都提供了有关上传进度的高度用户反馈。它们可能比 APC 或 uploadprogress 更容易实现,并且具有由其他人全面构建和测试的优势。
Plupload 支持多种上传引擎,包括 HTML5、Gears、Flash、Silverlight、oldschool HTML4 等。在 HTML5、Flash 和 Silverlight 之间,您几乎覆盖了 100% 的受众。它还允许您订阅事件并让您自己的代码发挥作用。例如,如果您需要服务器端文件上传进度信息,您可以让客户端定期将更新发送到不同的脚本。如果您经常让客户上传巨大文件并且您想实时了解它,这将非常有用。
tl;dr: 上传很难,让我们使用客户端吧!
The core problem is that RFC 1867, the specification for file uploads over HTTP via the
multipart/form-data
MIME type, does not provide any method for providing file upload progress.A file upload is actually just a fancy form submit. CGI scripts, PHP, and all other web technologies that rely on a front-end web server to first accept the request might not actually begin executing until the entire upload has completed. This means that they generally can't even know when the upload has started, only when it's been completed.
New versions of PHP's APC extension include a workaround for this problem that performs some level of black magic that allows it to know about uploads earlier. It only works as part of mod_php, though. The devs don't seem to have plans to support it under FastCGI.
Another server-side option would be the "uploadprogress" PECL extension. I'm not entirely sure what kind of black magic it uses. The source suggests that it actually hooks into the processing of the
multipart
MIME parts. (This suggests that at least some SAPIs stream form data to PHP as the client uploads it. I know that at least some FastCGI servers buffer the entire request before passing it along, so this might not work for you. YMMV.)Both of these options are for normal file uploads. Ajax -- or rather, XMLHttpRequest -- does not support file upload operations. Most of the workarounds in this area involve creating an iframe and submitting a form there, and that also implies someone else's client-side work. If you're going to go through that level of hoop jumping, you'd may as well use one of the modern file upload widgets.
Personally, I use Plupload, a Javascript widget that can work with everyone's favorite Javascript library, jQuery. Some others swear by Uploadify. Regardless, both of these widgets offer a high degree of user feedback as to upload progress. They are likely to be easier for you to implement than APC or uploadprogress and have the advantage of being built and tested thoroughly by other people.
Plupload supports multiple upload engines, including HTML5, Gears, Flash, Silverlight, oldschool HTML4 and more. Between HTML5, Flash and Silverlight, you've pretty much just covered 100% of your audience. It also allows you to subscribe to events and have your own code perform magic. For example, if you need server-side file upload progress information, you can have the client regularly send updates to a different script. This would be useful if you regularly have clients uploading huge files and you want to know about it in real time.
tl;dr: Uploading is hard, let's go client-side!
是的,我也不喜欢这种“作弊”方法,在我看来,最好的方法是使用 APC 及其方法 apc_fetch
使用 ajax 进行 apc_fetch,并使用指定上传的唯一键,将返回您需要的内容。即上传的字节数/总字节数。
然后简单地用javascript做一个进度条。
我听说 chrome 和 safari 不允许您在上传后进行 ajax 调用,工作范围包括使用 iframe 与 apc 标识符进行调用。
Yeah,I dont like that "cheating" method either, In my opinon, the best method is to use APC , and its method, apc_fetch
Using ajax to make a apc_fetch, with a unique key specifying the upload, will return what you need .. ie bytes uploaded / total bytes.
Then simply do a progress bar with javascript.
I have heard chrome and safari dont allow you to do ajax calls during post upload, the work arround includes using an iframe to do the calls with the apc identifier.