2

Here is what i need to do. So i have this code

 <input type="file" id="uploadImage" name="image" />
    <input type="submit" id="ImageName" name="submit" value="Submit">

So when i click the "Choose.." button to browse an image, i want the path of that image to be saved in a variable.

Or basically what i need to do is to make this path to be src of an image.

$('#Imageholder').append('<img src="" class="ImgCoords" >');

Any ideas?

Sorry if the explanation is bad :

Rocket Hazmat
  • 204,503
  • 39
  • 283
  • 323
  • So, you want the user to select an image from their computer and then display it on the screen? – Rocket Hazmat Sep 25 '13 at 15:02
  • Maybe you can check out the w3c-specs for that: http://www.w3.org/TR/2012/WD-html5-20121025/the-input-element.html#dom-input-src :`attribute DOMString src;` – AvL Sep 25 '13 at 15:04
  • 1
    Something like this: http://stackoverflow.com/questions/4459379/preview-an-image-before-it-is-uploaded – Yuriy Galanter Sep 25 '13 at 15:06

2 Answers2

5

Here is an example that i have made , adapt it to your need :

DEMO HERE

I added a button remove uploaded file,if you don't like show/hide effect just delete slow , Image is shown only on upload:

$('#blah').hide();
$('#remove').hide();  
function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#blah').attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#imgInp").change(function(){
        if( $('#imgInp').val()!=""){

            $('#remove').show();
            $('#blah').show('slow');
      }
        else{ $('#remove').hide();$('#blah').hide('slow');}
        readURL(this);
    });


    $('#remove').click(function(){
          $('#imgInp').val('');
          $(this).hide();
          $('#blah').hide('slow');
          $('#blah').attr('src','http://upload.wikimedia.org/wikipedia/commons/thumb/4/40/No_pub.svg/150px-No_pub.svg.png');
});

HTML Code:

<form id="form1">
    <input type='file' id="imgInp" name='image'/>
    <img id="blah" src="#" alt="your image" />
</form>
Charaf JRA
  • 7,659
  • 1
  • 27
  • 41
1

If you are using HTML5,you can.

See here for the File API. by W3C

The below key line from API

  var file = document.getElementById('file').files[0];

But if you are not using HTML5

For security reasons you cannot do that.html doesn't have access to file system information.

MIght be useful :Preview an image before it is uploaded

Community
  • 1
  • 1
Suresh Atta
  • 114,879
  • 36
  • 179
  • 284