When uploading files in IE, the local path becomes "C: \ fakepath \".

Source: Internet
Author: User

When you use the <input id = "file_upl" type = "file"/> Control to upload a file, you sometimes need to obtain the local path of the file and display it to the customer, in this case, you can obtain the local file path in this way:

Document. getelementbyid ('file _ upl '). Value

This is no problem in IE7 and earlier IE browser versions, but it won't work when it comes to IE8. In IE8, you will only get this path: "C: \ fakepath \ xxx ", where XXX is your file name.

What's going on?

Originally, for security reasons, IE8 blocked the actual local file path during file uploading and replaced it with "C: \ fakepath.

But what should we do if we want to obtain the real local file path?

You can obtain the actual path by setting browser security options:

Internet Options-> Security-> Custom Level-> include local directory path when uploading local files to the server-> select "start"-> OK

However, as developers, we cannot expect customers to do this, so we must passCodeSolve this problem.

For example, the HTML code of my file upload control is:

<Input id = "file_upl" type = "file"/>

In the js code, I can obtain the actual file path as follows:

VaR file_upl = Document. getelementbyid ('file _ upl ');
File_upl.select ();

VaR realpath = Document. selection. createRange (). text;

If we use Ext, we will use Ext. UX. form. filefield component. If the ID assigned to it is "file_upl", the above problem will also occur. The solution is the same, but we can get the input with the type of file, the elements obtained based on "file_upl" are not what we need at all. Ext automatically generates an input with the type of file. You can see this input through debugbar or other similar tools, its ID is assigned to Ext. UX. form. filefield ID plus "-file" suffix, that is, "file_upl-file", so get the real file path JS Code is:

VaR file_upl = Document. getelementbyid ('file _ upl-file ');
File_upl.select ();

VaR realpath = Document. selection. createRange (). text;

This solution is only for IE browsers and is not discussed in other browsers for the moment.

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.