• Home
  • About
  • Resume

FileUpload and htmlfile: Access is denied error

By , 03/10/2009 4:47 PM

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 Responses to “FileUpload and htmlfile: Access is denied error”

  1. Mar says:

    thanks…..you saved me

  2. sourabh says:

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

  3. Anna says:

    @sourabh
    Sourabh, that was simple and brilliant. You saved me. Thanks!

  4. Sathishkumar says:

    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

  5. Sathishkumar says:

    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”

  6. kazuya says:

    @Sathishkumar

    hi did u already solve this proble….we have the same problem…pls share if you have something there

  7. cyke says:

    you save me sourabh, just change the name attribute for an id attribute and it works!

  8. Simus says:

    Thanks for saving me many wasted hours!

Leave a Reply

Panorama Theme by Themocracy