问题
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! :)
回答1:
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.
回答2:
You have different way of achieving this :
One is to use https://github.com/lardawge/carrierwave_backgrounder , so you'll know if the image is being processed with the new column it uses.
You could also use the JS File API as described here : http://kellishaver.tumblr.com/post/21428272282/using-the-file-api-to-preview-images-before-uploading
And I guess there are other options.
来源:https://stackoverflow.com/questions/12735058/rails-background-image-upload