Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preview image before form submit

I have ImageField in my form. Is there any way to show chosen file before submitting a form? Maybe it is possible in jQuery?

I read that I can somehow access this files with request.FILES, but I think it will be empty before I will submit form.

like image 662
szaman Avatar asked Jul 07 '11 08:07

szaman


People also ask

How do I preview an image before uploading HTML?

html. We have two main elements that we will interact with using JavaScript. First, we have a division element that contains the “img” tag. Using Jquery, we will change the “src” attribute of the “img” tag on upload to preview the image.

How do I preview multiple images before uploading?

The HTML Markup consists of an HTML FileUpload control and a DIV which will be used for displaying live preview of multiple images. Note: For the HTML INPUT FileUpload control it is very important to set the HTML5 multiple property to multiple in order to allow multiple file selection.

What is a preview image?

A thumbnail also means a small and approximate version of a full-size image or brochure layout as a preliminary design step. This is also referred to as a preview image and is a smaller version of the original image.


1 Answers

try this one.

function upload_img(input) {
  if (input.files && input.files[0]) {
    var src = URL.createObjectURL(input.files[0])
    $('#img_id').attr('src', src)
  }
}

AND HTML is below.

Thanks.

<form id="form_img">
  <input type='file' onchange="upload_img(this);" />
  <img id="img_id" src="#" alt="your image" />
</form>

This may helpful to you.

like image 154
Chandresh M Avatar answered Oct 01 '22 21:10

Chandresh M