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.