Show file preview of input
I'm trying to show a simple preview of a file, once it is selected in a form input. The image that I am trying to write a source to is appended after the page is loaded but before the image is displayed.
- <form>
- <div>
- <div>
- <input type="file">
- </div>
- ...
- <img class="preview" src="#" alt="Preview Image" />
- </div>
- </form>
- function myFiles(files) { // files is the selected file
- console.log('File: ' , files[0]); // has name, type, size, etc.
- var ipreview = $(self).parent().parent().find('.preview'); // $self is the input tag
- console.log('Ipreview: ' , ipreview); // It's targeting the image node
- ipreview.src = URL.createObjectURL(files[0]);
- console.log('Src: ' , ipreview.src); // Shows something like this: Src: – "blob:http://mysite.com/a3486686-0995-43dc-9397-67d9b5f92614"
- }
What am I doing wrong?