Best way to show image previews before upload in rails & carrierwave

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 12:15:49

问题


I've been using rails for past few days and wanted to know what is best way to show image previews before upload in rails & carrierwave.

I came across a few options like using plupload or jquery file upload or using uploadify.


回答1:


If you only need image preview in a form before upload, you (as me) will see that JQuery Upload plugin is just too much complex and not so easy to run properly (I was able to see the preview, but then I couldn't upload the picture).

http://saravani.wordpress.com/2012/03/14/preview-of-an-image-before-it-is-uploaded/

It's simple and fast to code.

I put the code here just in case the source dies:

Script:

    <!-- Assume jQuery is loaded -->
    <script>
      function readURL(input) {
        if (input.files && input.files[0]) {
          var reader = new FileReader();

          reader.onload = function (e) {
            $('#img_prev')
              .attr('src', e.target.result)
              .width(150)
              .height(200);
          };

          reader.readAsDataURL(input.files[0]);
        }
      }
    </script>

In the HTML:

    <!--[if IE]>
      <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    </head>
    <body>
      <input type='file' onchange="readURL(this);" />
      <img id="img_prev" src="#" alt="your image" />
    </body>



回答2:


You can use the plugin jQuery File Upload which it's a full solution, but if you need something less robust, you can also try using FileReader directly, like this, so you can customize the functionality to whatever you need.




回答3:


According to the documents image_cache is showing what you are uploading.

<%= f.hidden_field :image_cache %>


来源:https://stackoverflow.com/questions/11485967/best-way-to-show-image-previews-before-upload-in-rails-carrierwave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!