Access Denied



I have a ASPX page which has a ListBox & a Button. The ListBox lists
all the files & directories existing in a directory on the server. I
want to give users the option to download files from the server.

When a user selects a file from the ListBox & clicks the button to
download the selected file, I am getting the following error:

Access to the path 'c:\inetpub\wwwroot\Folder1\MyDir' is denied.

The directories & files that the ListBox lists reside in the 'MyDir'
directory. This is the OnClick event function of the button:

<%@ Import Namespace="System.Net" %>

Sub DwnldFile(obj As Object, ea As EventArgs)
Dim strFileName As String
Dim wClient As New WebClient

strFileName = Request.Form("lstFD")
wClient.DownloadFile(Server.MapPath("Folder1\MyDir"), strFileName)
End Sub

I have gone through a no. of posts to resolve this issue but haven't
had any luck so far.

Can someone please tell me where I could be going wrong?

bruce barker

webclient is used to read a file from the web. you are asking it to
download from a file path which is not normally allowed.

i believe you are trying to read the file and send to the browser, which
would be:


you will still need to set the content-type header.

-- bruce (


Bruce, I guess you have got it wrong. What I am trying to do is allow
users to DOWNLOAD a file from a directory that resides on the server so
that they can SAVE that file on their local machine.

Mark Fitzpatrick

I don't think Bruce has it wrong. The only thing that may change that is,
where is this aspx page located? If it's in the the web application Folder1,
then Bruce is completely right. You shouldn't be using the webclient at all.
The webclient would be used for you to download a file into the application
from somewhere else. It would not make this file available to the user at
all. That is what the Response.TransmitFile does that Bruce mentioned. It
basically loads the file that exists on the server in that particular
location and dumps it to the output stream as a file, which then gets the
browser to prompt the user as to where to save it.

The reason you are getting this error is because the webclient is
downloading a file from the server, and saving it on the server. It is not
interacting with the user at all. So what you are doing is taking the file,
and saving it as a new file though I'm not sure where it would attempt to
save it, probably the application root. You're getting an access denied
error because the ASPNET user account doesn't have write permissions to the
folder it's attempting to save to, which is normal as the ASPNET user
account should only have very limited access rights and only have write
ability to certain non-critical directories.


Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006


The only thing that may change that is,
where is this aspx page located?
If it's in the the web application Folder1,
then Bruce is completely right.

Mark, the ASPX page that has the ListBox & the Button is located in
Folder1. Assume that this ASPX file is named, say, FilesDirs.aspx.

Now when I use Bruce's suggestion, what I find is after selecting a
file from the ListBox in FilesDirs.aspx, when I click the Button & post
the Form, the contents of the selected file get merged with the
contents of FilesDirs.aspx.

For e.g. suppose the ListBox in FilesDirs.aspx lists a file named
ServerPage.aspx. Assume that ServerPage.aspx has a Label whose text is,
say, 'THIS IS SERVER PAGE' (note that ServerPage.aspx resides on the
server). I select ServerPage.aspx from the ListBox & click the Button.
Now when FilesDirs.aspx posts & comes back to FilesDirs.aspx, what I
find is FilesDirs.aspx first displays the contents of the file
ServerPage.aspx & then displays the contents of FilesDirs.aspx.

So in this case, after FilesDirs.aspx posts, the Label (that exists in
ServerPage.aspx) with the text THIS IS SERVER PAGE' gets rendered first
(i.e. at the very top of the page) & after that the ListBox & the
Button that exist in FilesDirs.aspx get rendered.

In other words, prior to posting the Form in FilesDirs.aspx,
FilesDirs.aspx displays only the ListBox & the Button but after posting
the Form, FilesDirs.aspx first displays the Label with the text 'THIS
IS SERVER PAGE' & below this Label, the ListBox & the Button get
displayed. The browser isn't prompting me to save the file in my hard

Any idea what could be going wrong???


OK...Mark...I got it. I was missing 2 very important lines - the
Response.AddHeader line & the Response.End line. This is it:

Sub DwnldFile(obj As Object, ea As EventArgs)
Dim strURL As String

strURL = Request.Form("lstFD")
Response.AddHeader("Content-Disposition", "attachment;filename=" &

Response.TransmitFile(Path.Combine("Folder1", strURL))
End Sub

Mark, what I found is if I replace

Response.TransmitFile(Path.Combine("Folder1", strURL))


Response.WriteFile("Folder1\" & strURL)

then also the code works fine. Then what's the difference between
TransmitFile & WriteFile? Also why use Path.Combine when simply
TransmitFile or WriteFile does the needful?

Could you please clarify these 2 doubts?

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question