FileUpload and htmlfile: Access is denied error

If you use ASP.NET FileUpload control or <INPUT TYPE=”FILE” HTML> control in Internet Explorer on Windows XP SP2 or later, trying to enter file path manually may cause “htmlfile: Access is denied error” error if entered string is not well formed local or UNC path.

The reason is that the control in Internet Explorer after version 6 in Windows XP SP2 does not allow relative path and if the form is submitted via Javascript form.submit method (like for example a __doPostback() call in ASP.NET) you will get untrappable error from above, if the form is submitted by an <INPUT TYPE=”SUBMIT> button you won’t get any notice at all.

The way to work around this issue is to catch invalid file name prior form being sumbitted. Add onclick event to your submitting button (OnClientClick if this is ASP.NET server side button) and add following code to it:

onclick=”if (!FilePathIsValid()) return false;”

then add following JavaScript function to your code:

function FilePathIsValid() {
 var reg = /^(([a-zA-Z]:)|(\))(\{1}|((\{1})[^\]([^/:<>”|]*))+)$/g
 if (!reg.test(document.getElementById(‘xfuFile’).value)) {
    alert(‘Please enter valid file path’);
    return false
 } else
   return true
}

where ‘xfuFile’ is the name of your FileUpload control. The function uses Jens K. Suessmeyer’s RegEx Expression to test if the file path is well formed. If it’s not – user gets error message and the form is not submitted.

8 replies on “FileUpload and htmlfile: Access is denied error”

  1. remove name attribute from the file control and this error will go..
    njy

  2. My Javascript function is

    This is my file upload control .This can be html file control as well.Both are same.

    function BrowseClick()
    {
    var formObject = document.forms[0];
    formObject[‘fileBrowse’].click();
    var addedFilePath = formObject[‘fileBrowse’].value;
    }

    Inside this function, I am making the fileUpload control click event to fire.
    I am getting the exception “htmlfile: Access is denied error”

    when I looked into the dynamically created html code for my web page, it has

    Sourabh?? How did u removed the name attribute and make it to work. name attribute automatically gets added when the page is rendered as html. plz guide me

  3. Sorry..following code missed in my last post as I pasted the html code.

    when I looked into the dynamically created html code for my web page, it has
    “input type=”file” name=”fileBrowse” id=”fileBrowse” run=”server”

Leave a Reply

Your email address will not be published. Required fields are marked *