0

I'm attempting to grab the value from the file input in html and placing it immediately into a div I have like so:

Html:

    <div class='pushLeft noOverflow' id='areaUpload'>
        <input type='file' id='uploadFileHere' />
    </div>

jQuery:

    $(document).on("change", "#uploadFileHere", function(){
        $("#areaUpload").html("<img src='" + $("#uploadFileHere").val() + "' />");

    });

The problem I'm having is that the image doesn't come up. I checked the file path and it was returning: C:\fakepath\filenamehere.jpg

So how would I be able to fix this issue?

3 Answers 3

3

You can use FileReader for it, Try this.

$(document).on("change", "#uploadFileHere", function(){
      var fileInput = this;
      if (fileInput.files && fileInput.files[0]) {
            var fileReader = new FileReader();

            fileReader.onload = function (e) {
               $("#areaUpload").html("<img src='" +e.target.result + "' />");
            }

            fileReader.readAsDataURL(fileInput.files[0]);
        }
});

Check this DEMO

Thanks to @FabrícioMatté, For this JsFiddle Demo

Should note that older browsers (IE < 10) does not support this API

Sign up to request clarification or add additional context in comments.

2 Comments

Beat me to making the example, nicely done. Here's a fiddle for your code. Should note that older browsers (IE < 10) does not support this API.
Thanks for giving me the answer I was looking for.
3

You need to upload the file to server and save it on server directory within website. Then give the relative or absolute path to image tag.

2 Comments

Thanks Doh, Your turn is not far :)
Hey guys, I'm not trying to upload content to my server. I just want to give the impression that it is. That's why I'm just trying to grab the value instead.
1

Sir I think you should first use some ajax thing (like POST action of a form) and upload the image on the server, then get the actual URL from server and put it in the src attribute value. You can search for an Image uploader plugin and see how they work in these situations.

Hope it helps you. Cheers

6 Comments

I'm able to do that, but I am not trying to upload content to my server. I'm just trying to give the impression that it is.
impression of what? loading a resource from client that has a serious security issue?! you should run some validation on the file and ofcourse you have not the permission on the client side to do that. First an handler should recieve whatever user is trying to upload.
First and foremost, I understand your concern, however, I did ask a specific question. You did not answer my answer specifically. For your information, this is not going to go live, this is just a demo I'm giving to my co-workers. I'm well aware of the security issues that come with file uploads.
Evil is a evil, no matter how beautiful they would be. Wish you the bests. Cheers
I understand that as well, but when you have time constraints, and you are the one who's controlling the outcome, especially locally, there is no problem with it,
|

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.