Reputation: 9289
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!
Upvotes: 1
Views: 2822
Reputation: 12923
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:
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
UPDATE AGAIN I stripped out the path so you just get the file name:
Upvotes: 2