How to preview a downloaded image using a clip in a ruby ​​on rails

Basically, I want the user to be able to preview the uploaded image before sending.

In the controller I have

def index @temp = Temp.new end 

Since @temp is not saved, can I use the solution from Rails: Folder and preview . If not, can I run javascript or something to run some ruby ​​code?

This is my html:

= form_for @temp ,: url => temp_path ,: html => {: multipart => true} do | form |

  = form.file_field :image 

= image_tag @ temp.image.url

= image_tag @ temp.image.url (: medium)

= image_tag @ temp.image.url (: thumb)

+6
source share
1 answer

If you understood correctly that you want to view the image before it is actually downloaded, right? You can do this using javascript.

By setting the width and height attributes of the image tag, you can simulate a thumbnail.

 $(function() { $('#pictureInput').on('change', function(event) { var files = event.target.files; var image = files[0] var reader = new FileReader(); reader.onload = function(file) { var img = new Image(); console.log(file); img.src = file.target.result; $('#target').html(img); } reader.readAsDataURL(image); console.log(files); }); }); 

and html:

 <form> <input type="file" id="pictureInput"> </form> <div id="target"> </div> 

you can check it on the code password

+27
source

Source: https://habr.com/ru/post/974271/


All Articles