简体   繁体   English

Ruby on Rails:使用进度条上传文件,并在上传之前检查文件大小

[英]Ruby on Rails: upload files with progress bar and checking of filesize before upload

Are there any gems / plugins that allow for a slick implementation? 是否有任何适用于精巧实现的gem /插件? Bonus for allowing multiple uploads =) 允许多次上传的奖励=)

The generic mechanism for uploading is to encode the entirety of the file to be uploaded within the HTTP request entity. 上载的通用机制是在HTTP请求实体中对要上载的文件进行编码。 Therefore, by the time the server-side application even sees the request, the file has already been uploaded (although some servers permit the optimization that the server-side processing can begin before the entirety of the HTTP request has been transmitted to the server, but will block if the application needs data from the HTTP request that has not yet been received). 因此,在服务器端应用程序甚至看到该请求时,该文件已经上传(尽管某些服务器允许进行优化,以使服务器端处理可以在将整个HTTP请求传输到服务器之前开始,但如果应用程序需要来自HTTP请求的尚未接收到的数据,则会阻止)。

There are several Flash-based uploaders that permit uploading files in separate HTTP requests; 有几个基于Flash的上传器,它们允许在单独的 HTTP请求中上传文件。 they require that the server application have a separate endpoint accepting HTTP requests particular to uploading files. 它们要求服务器应用程序具有一个单独的终结点,以接受特定于上传文件的HTTP请求。 Some even implement progress bars, multiple file uploads, client-side constraints on content-type and content-length, and more besides. 有些甚至实现了进度条,多个文件上传,客户端对内容类型和内容长度的限制等。 At random: YUI Uploader , Uploadify , FancyUpload . 随机: YUI UploaderUploadifyFancyUpload

I used Uploadify which is written as a jQuery plugin, as the multi-file uploader for my blogging app called "Rehash" ( source code ), check the ProjectsController#new_upload action. 我使用了作为jQuery插件编写的Uploadify,作为我的博客应用程序名为“ Rehash”( 源代码 )的多文件上传器,请检查ProjectsController#new_upload操作。 We have used this on a few projects and keep improving our implementation. 我们已在一些项目中使用了此功能,并不断改进我们的实施。 The tricky part with any Flash multi-file uploader (like Uploadify or FancyUpload) is dealing with Rails' cross-site request forgery protection and cookies. 任何Flash多文件上传器(例如Uploadify或FancyUpload)的棘手部分是处理Rails的跨站点请求伪造保护和cookie。 You want to ensure the request comes from the application and you may want to authenticate the upload. 您要确保请求来自应用程序,并且可能要对上传进行身份验证。 In Rehash I only allow the site administrator to upload for example, so I need to check both the session key and the authentication token. 例如,在Rehash中,我仅允许站点管理员上载,因此我需要同时检查会话密钥和身份验证令牌。 John Nunemaker has a nice blog post on Rails Tips on getting Uploadify set up, outlining the current best practice of dealing with the Flash/session stuff as Rack middleware, using Paperclip for file uploads, which is our same setup. John Nunemaker 在Rails上有一篇不错的博客文章,关于设置Uploadify的技巧 ,概述了使用Paperclip进行文件上载的将Flash /会话内容作为Rack中间件进行处理的当前最佳实践,这与我们的设置相同。 In the end you have a nice multi-file uploader with style-able progress bars, but it is a good amount of work initially. 最后,您将获得一个不错的多文件上传器,其中包含可设置样式的进度条,但最初需要大量工作。

Uploadify has a sizeLimit option as well, check the docs . Uploadify也具有sizeLimit选项, 请检查docs

I use the YUI uploader . 我使用YUI上传器 Industrial strength, large community, book etc. I had to roll my own integration into Rails, but wasn't hard. 工业实力,庞大的社区,书籍等。我不得不将自己的集成整合到Rails中,但并不难。

PS, check out the widget's examples, listed on the far right side of the page. PS,查看页面最右侧列出的小部件示例。

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

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