0

I need to show upload image before upload using jquery but in chrome path shown fake path like this:

C:\fakepath\myImage.jpg

and in firefox it's shown only file name:

myImage.jpg

so how can me preview my image file before uploading

<input type="file">
<img src="" class="preview" />

if that is impossible.. what about HTML5?

Abudayah
  • 3,816
  • 7
  • 40
  • 60

2 Answers2

6

I don't know of a jQuery function (sorry), but it is possible with HTML5, and most browsers support this HTML5 function. I found a piece of code in HTML5rocks (1):

<style>
  .thumb {
    height: 75px;
    border: 1px solid #000;
    margin: 10px 5px 0 0;
  }
</style>

<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>

<script>
  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Render thumbnail.
          var span = document.createElement('span');
          span.innerHTML = ['<img class="thumb" src="', e.target.result,
                            '" title="', escape(theFile.name), '"/>'].join('');
          document.getElementById('list').insertBefore(span, null);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>

Good luck :)


(1) HTML5rocks - Reading Files: http://www.html5rocks.com/en/tutorials/file/dndfiles/

Adrià Vilanova
  • 384
  • 4
  • 20
0

You can't. You need to upload image using ajax, for example, return image url and finally display it.

Pavel Reznikov
  • 2,968
  • 1
  • 18
  • 17