1

I'm following the answer HERE to create a file upload with image preview.

I have it working, but can't figure out how to place the file path in a separate div. Here's the js:

function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('[data-label="UploadPreview"]').children('img').attr('src', e.target.result);
        }

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

$('#UploadInput').change(function(){
    readURL(this);
});

This is the element where I want to place the file path text:

<div data-label="UploadPath"></div>

I have tried setting .text on the reader.onload function as such

$('[data-label="UploadPath"]').text(e.target.result);

But the result appears to be the base64 value of the image. Can you help me figure out how to include the path of the image in the data-label="UploadPath" div? Thanks for your ideas! BTW, I'm new to jQuery so an example of how to include in my current code would help a lot. Thanks!

Community
  • 1
  • 1
Nick B
  • 8,356
  • 12
  • 55
  • 100

1 Answers1

2

The way you have that written you're trying to target an img tag as a child of the data-label=UploadPath, so if you put in an empty <img/> then the script can append the image like so:

JSFIDDLE

You can also append an <img/> tag instead of leaving a blank one in there, your choice

UPDATE:

Just grab the value when uploading, you can use regex to strip out the path before the filename if needed

UPDATED FIDDLE

UPDATE AGAIN I stripped out the path so you just get the file name:

UPDATED FIDDLE 2

jmore009
  • 12,570
  • 1
  • 17
  • 33
  • No i mean I want to style the text that says where the path is located on the filesystem. So I want to put the text in a separate div: `div data-label="UploadPath"`. Does that make more sense? I have the upload preview working fine, I just want to put the text that says the path somewhere else.. – Nick B May 29 '14 at 19:40