UnauthorizedAccess with streamreader

J

jeff31162

Hi all
Using vs2003 and C# in a web app.
I want the user to be able to upload a file.
I have an htmlFileField (<input>) on my form.

Then in my C# codebehind, I use fileChooseHtml.PostedFile.FileName to get
the location of the file (the user chose) to read in.

Then I use the following code to actually read the file:
using (StreamReader sr = new StreamReader(@sFilePath))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
sReturnLine += line;
}
}

and I get the error:
System.UnauthorizedAccessException: Access to the path "C:\Documents blah
blah blah...

So, yes, I could give permissions to ASP.NET (I have done it on other
folders and the code works fine) but, am I going to have these same
permission issues when I put this on the web and a user wants to upload a
file from their PC?

I havent gotten that far and might as well find out now, if it is going to
break when I try to deploy it.

Thanks
Jeff
 
P

parez

Hi all
Using vs2003 and C# in a web app.
I want the user to be able to upload a file.
I have an htmlFileField (<input>) on my form.

Then in my C# codebehind, I use fileChooseHtml.PostedFile.FileName to get
the location of the file (the user chose) to read in.

Then I use the following code to actually read the file:
using (StreamReader sr = new StreamReader(@sFilePath))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
sReturnLine += line;
}

}

and I get the error:
System.UnauthorizedAccessException: Access to the path "C:\Documents blah
blah blah...

So, yes, I could give permissions to ASP.NET (I have done it on other
folders and the code works fine) but, am I going to have these same
permission issues when I put this on the web and a user wants to upload a
file from their PC?

I havent gotten that far and might as well find out now, if it is going to
break when I try to deploy it.

Thanks
Jeff

check this out...

http://www.4guysfromrolla.com/webtech/091201-1.shtml

The file you are trying to read is on the clients computer and your
code is running on the webserver.
 
J

Jeff User

check this out...

http://www.4guysfromrolla.com/webtech/091201-1.shtml

The file you are trying to read is on the clients computer and your
code is running on the webserver.

Thanks, but....
I was using the stream reader because I need the file in a local
variable so that I can manipulate the contents. The article that you
refer to only states how to save it directly to the server. I need to
evaluate it and make modifications to it first.

Any other ideas?

Thanks again
Jeff
 
J

Jon Skeet [C# MVP]

Jeff User said:
Thanks, but....
I was using the stream reader because I need the file in a local
variable so that I can manipulate the contents. The article that you
refer to only states how to save it directly to the server. I need to
evaluate it and make modifications to it first.

Any other ideas?

Yes - look at the API that the article is using. It uses HtmlImputFile
and HttpPostedFile, which lets you get at the data being posted. Just
because they happen to save it straight to disk doesn't mean that's all
you can do with it.
 

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

Top