Rails背景图片上传 [英] Rails background image upload

查看:192
本文介绍了Rails背景图片上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用carrierwavegem上传图片文件,也使用carrierwave backgrounder来处理上传+ RMagick在后台处理图片(我使用Delayed Job)。

一切都像一个魅力。除了一件事 - 我想上传图片,并在上传表单中显示它们(所以,所有事情都发生在同一页面上)。所以,基本上,会发生什么事情 - 因为上传和图像处理相对较慢,在发布之后呈现页面的时候 - 图像本身还没有准备好:因此image_tag显示了资产链接(href =)资产)。等待2-3秒(刷新几次)后,图像出现在屏幕上。所以,我的问题是 - 我可以以某种方式显示动画(或文本,或其他图片 - 无所谓)代替破碎(尚未提供)图像,一旦准备就绪 - 将图像显示给用户。这可以做到没有AJAX?或者我应该考虑重新使用AJAX?



对不起,长话短说。希望对您有所帮助!

预先致谢!我的解决方案非常简单:我观察数据库中是否有任何包含tmp字段的图像(st):

tmp!= null)。如果是这样 - 他们正在后台处理,所以我使用setInterval函数,不断询问tmp是否为null:一旦它为空 - 处理是肯定的。如果没有 - 加载动画正在运行,而不是上传的图像。非常简单。

I'm using "carrierwave" gem for uploading image files and also "carrierwave backgrounder" to process the uploading + processing the image with RMagick in the background (I use Delayed Job for this).

Everything works like a charm. Except one thing - I want to upload the images and show them right under the upload form (so ,everything happens on the same page). So, basically, what happens - since the uploading and image-processing are relatively slow, by the time the page has been rendered after the posting - the image itself is not yet ready: so image_tag shows broken "assets" link (href = "assets"). After waiting 2-3 seconds (couple of refreshes), the image appears on the screen.

So, my question would be - can I somehow show the "LOADING" animation (or text, or another picture - doesn't matter) instead of broken (not yet available) image, and once it is ready - to display the image to the user. Can this be done without AJAX ? Or should I consider re-doing it with AJAX ?

Sorry for the long story. Hope for your help!

Thanks in advance! :)

解决方案

My solution is pretty simple: I observe if I have any images containing "tmp" field in the database (s.t tmp != null). If so - they are being processed in the background, so I use setInterval function, that keeps asking if tmp is null or not: Once it is null - processing is done for sure. if not - loading animation is running instead of the uploaded image. Quite simple.

这篇关于Rails背景图片上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆