0

I'm not sure this is possible without some kind of server interaction but I am hoping it is for prototyping purposes. I have a file upload form field. I would like user to be able to select image from hard drive and jquery will put that image into a div in the DOM.

Here is my code

<div class="label">Upload Your Own Logo</div>
  <input name="upload" type="file" id="logo-upload">

Div where image should go

<div class="user-logo">logo.png</div>

2 Answers 2

1

If the user's browser has HTML5 FileReader API then this is possible:

var file = document.getElementById('logo-upload').files[0],
    reader = new FileReader();
reader.readAsDataURL(file);

reader.onloadend = function(e) {
    var image = $('<img>').attr('src',e.target.result);
    $(image).appendTo.('.user-logo');
}
Sign up to request clarification or add additional context in comments.

Comments

0

You don't need AJAX for that. In fact you don't even need jquery.

<input type="file" name="ss" id="fileUpload" onchange="dothis()"/>
<div id="picDiv"></div>

use function dothis() to append the image you upload inside #picDiv

function dothis()
{
    var oImg=document.createElement("img");
    oImg.setAttribute('src', document.getElementById("fileUpload").value);
    oImg.setAttribute('alt', 'na');
    document.getElementById("picDiv").appendChild(oImg);
}

Don't forget to check if the file is valid image or not, which I leave it upto you.

3 Comments

I am getting a 404 on the image. "NetworkError: 404 Not Found - localhost:8080/Logo4U/cropped.jpg" is there a reason why it's looking for the image in the root folder?
what browser are you targeting?
All of them, but was initially testing in FF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.