Question

I'm new to Ruby.

I have a DIV in which there is a default image.

I want to upload another image on clicking over this image or div. I don't want to save this image to a database. Also I don't want to use form as most paperclip tutorials says (if there is way to do it without form).

Was it helpful?

Solution

You want to use data-urls. here's an example of how I've used it:

<div>
   <%= f.file_field :image, :id => "image" %>
   <output id="image_preview_output">
   </output>
</div>
<script>
$(document).ready(function() {
    // Check for the various File API support.
    if (window.File && window.FileReader && window.FileList && window.Blob) {
    } else {
        alert('The File APIs are not fully supported in this browser.');
    }
});
$('#image').change(function previewImage(evnt) {
    var file = evnt.target.files[0];
    // Only process image files.
    if (!project_featured_image.type.match('image.*')) {
        var reader = new FileReader();
        reader.onload = (function(theFile) {
            return function(e) {
                var span = document.createElement('span');
                span.innerHTML = ['<img id="image_preview" src="', e.target.result, '" title="', escape(theFile.name), '"/>'].join('');
                $('#image_preview_output').html(span);
            };
        })(file);
        // Read in the image file as a data URL.
        reader.readAsDataURL(file);
    }
});
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top