Getting fakepath in file browse

Yesterday I got into a weird problem.I was integrating the file upload via Ajax.For this I placed the normal "file" input in the HTML.I required to send the name of the file via JavaScript separately.The name of the file would be shown in a listing.This was working perfectly in Firefox browser,but the problem came when I was checking in IE8.Somehow the file path is getting entered in this way, " c:\fakepath\abc.jpg" .I was very confused with this "fakepath" what it is and why is the original path is not getting into the file input.I am sure many of you there is also facing the same problem.

The problem happens because,according to the latest HTML5 specification,the file upload control will not reveal the original file path of your computer.It will always show you "fakepath". You will also not get the path via JavaScript ,there also you will get this "fakepath".This specification is already implemented in IE8 and thus the problem happens.

Then what is the solution to it? Well in IE8 there is a workaround.If you can specify your site as trusted site,then you will not get this "fakepath",and it will reveal you the original path of your computer.So how to do it.Open your tools menu,then point to internet options,go to the security tab,under "select a zone to view or change security settings" click on trusted sites,then click on sites,here add your site with the add button,close the dialog via close button and click on ok button in the main window.Now restart your browser.If everything goes OK then you will get a green tick on the status bar if you open your site.Now click on the browser button and select a file.Hopefully you will see the full path and can also get it via JavaScript.

But how to get around if you are going to give the project to your client.Well simple write a message beside your file upload that whoever is uploading file via IE browser will have to mark the site as trusted before file upload or the other way round you can detect the browser version and type via JavaScript and show your message accordingly.

5 comments:

  1. Thank u for the solution. I got it by the following steps

    Go to tools ->internet options->security->trusted sites->sites->Enter
    http://localhost ->click add and
    uncheck require https

    ReplyDelete
  2. I believe that FireFox strips the path and all you get is the file name, and also that FireFox no longer uses the "trusted site" concept. I am still looking for a solution, because I am really new to the level of web programming, but it seems you have to avoid using JavaScript in these cases.

    ReplyDelete
    Replies
    1. Firefox versions are changing by leaps and bounds these days. Please suggest a solution to this problem in the new firefox versions,if you find any. That would help others also. Thanks.

      Delete