Question

WHAT I'M DOING

I'm trying to implement profile picture changing in php/jquery. When an image is uploaded the preview of the image is shown.

WHAT I REQUIRE

I want to get the src of the previewed image. When i use $('img').attr('src'); it gives the src of the old image and not the new previewed image.

SCRIPT

function readURL(input) {
     if (input.files && input.files[0]) {
      var reader = new FileReader();
      reader.onload = function (e) {
      var image =  $('#img').attr('src', e.target.result);
      }
      reader.readAsDataURL(input.files[0]);
     }
     }
    $("#imgInp").change(function(){
      readURL(this);
     });

HTML

 <div class="pic">

  <form id="form1" enctype="multipart/form-data">

   <img src="profile.jpg" width="150"  alt="profile" title="" id="img"/>

     <span>Change Picture?</span>

      <div class="up">

         <input type='file' name ="file" id="imgInp"  />

     </div>

         <input type="submit" name="picture" id="picture" value="Update"/>

    </form>

 </div>
Était-ce utile?

La solution

There is a new security "feature" in IE 8 that hides the real path of the selected file from JavaScript calls (it returns c:\fakepath). This seems to only be activated for "Internet" servers ... not Local intranet servers...

See here for more information about C:\fakepath

Edit

Also, you should consider removing this line of code (you don't need it in your script)...

reader.readAsDataURL(input.files[0]);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top