Solve the problem of Fakepath when uploading local files

Source: Internet
Author: User

First, you should look at what the Fakepath problem specifically refers to:

When you write the page, you need to select the local image to the server, while the selected picture is previewed in the page, the code is written like this:

$ ("input[type= ' file ']"). On (' Change ', function () {
    $ ('. Content '). attr (' src ', $ (this). Val ());
    Alert (This). Val ());
});

This is how I choose the Picture:

However, you will find the value actually added to the input box (the SRC value of the image is):

The Fakepath here is a string created by modern browsers to hide the actual path of the file. The next step is to find a way to find out the original path of the image, and the following code is the solution found on the Web:

$ ("input[type= ' file ']"). On (' Change ', function () {
    var ofreader = new FileReader ();
    var file = document.getElementById (' Input-file '). Files[0];
    Ofreader.readasdataurl (file);
    Ofreader.onloadend = function (ofrevent) {
        var src = oFRevent.target.result;
        $ ('. Content '). attr (' src ', src);
        alert (src);
    }
});

So the problem is solved.

Read more about a good solution to this problem see the great God's Blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.