Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

filestream 'could nor find a part of the path'

Status
Not open for further replies.

cbsm

Programmer
Oct 3, 2002
229
FR
I searched all afternoon - found lots of post here and there ... but error still here ...
I am trying to open a file (to store it in a database).
The error occurs here
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
If I run it on my machine (local), and try
to open a file
* c:\Project\MyFile.pdf
=> it works fine
* C:\Users\ME\Desktop\MyFile.pdf
=> access denied
If I run it from the web , I get the error
=> could nor find a part of the path

* In Web.config I have added : <identity impersonate = "true"></identity>
* Users will upload the files they want, I cannot ask them to change the files access right (I am not sure they have the rights to do it !)
* on the server, IIS V6.0 , Application Pools Identity is 'Network Services', I checked "Enable anonymous access" and "Integrated Windows authentication" on th Web Site.
* I am using Visual Studion 2008

If you need more information to help me out - just ask !!!
Thanks
 

* C:\Users\ME\Desktop\MyFile.pdf
because this is on your desktop, the location is by default secure to all accounts that are not you.
You will need to make sure you have EXPLICITLY given permission to the account that your webapp is running under (app pool of the specific website/app)
 
cbsm said:
* Users will upload the files they want, I cannot ask them to change the files access right (I am not sure they have the rights to do it !)

Are you trying to make an upload form then?



ASP.NET appears to have a file upload form control. Taken from the MSDN:

Code:
[COLOR=#000000]<%[/color][COLOR=#0000FF]@[/color][COLOR=#000000] [/color][COLOR=#800000]Page[/color][COLOR=#000000] [/color][COLOR=#FF0000]Language[/color][COLOR=#0000FF]="C#"[/color][COLOR=#000000] %>

[/color][COLOR=#0000FF]<[/color][COLOR=#800000]script[/color][COLOR=#000000] [/color][COLOR=#FF0000]runat[/color][COLOR=#0000FF]="server">[/color][COLOR=#000000]
    [/color][COLOR=#0000FF]protected[/color][COLOR=#000000] [/color][COLOR=#0000FF]void[/color][COLOR=#000000] Button1_Click([/color][COLOR=#0000FF]object[/color][COLOR=#000000] sender, [/color][COLOR=#2B91AF]EventArgs[/color][COLOR=#000000] e)
    {
        [/color][COLOR=#0000FF]if[/color][COLOR=#000000] (FileUpload1.HasFile)
            [/color][COLOR=#0000FF]try[/color][COLOR=#000000]
            {
                FileUpload1.SaveAs([/color][COLOR=#A31515]"C:\\Uploads\\"[/color][COLOR=#000000] + 
                     FileUpload1.FileName);
                Label1.Text = [/color][COLOR=#A31515]"File name: "[/color][COLOR=#000000] +
                     FileUpload1.PostedFile.FileName + [/color][COLOR=#A31515]"<br>"[/color][COLOR=#000000] +
                     FileUpload1.PostedFile.ContentLength + [/color][COLOR=#A31515]" kb<br>"[/color][COLOR=#000000] +
                     [/color][COLOR=#A31515]"Content type: "[/color][COLOR=#000000] +
                     FileUpload1.PostedFile.ContentType;
            }
            [/color][COLOR=#0000FF]catch[/color][COLOR=#000000] ([/color][COLOR=#2B91AF]Exception[/color][COLOR=#000000] ex)
            {
                Label1.Text = [/color][COLOR=#A31515]"ERROR: "[/color][COLOR=#000000] + ex.Message.ToString();
            }
        [/color][COLOR=#0000FF]else[/color][COLOR=#000000]
        {
            Label1.Text = [/color][COLOR=#A31515]"You have not specified a file."[/color][COLOR=#000000];
        }
    }
[/color][COLOR=#0000FF]</[/color][COLOR=#800000]script[/color][COLOR=#0000FF]>[/color][COLOR=#000000]

[/color][COLOR=#0000FF]<[/color][COLOR=#800000]html[/color][COLOR=#000000] [/color][COLOR=#FF0000]xmlns[/color][COLOR=#0000FF]="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL][/color][COLOR=#000000] [/color][COLOR=#0000FF]>[/color][COLOR=#000000]
[/color][COLOR=#0000FF]<[/color][COLOR=#800000]head[/color][COLOR=#000000] [/color][COLOR=#FF0000]id[/color][COLOR=#0000FF]="Head1"[/color][COLOR=#000000] [/color][COLOR=#FF0000]runat[/color][COLOR=#0000FF]="server">[/color][COLOR=#000000]
    [/color][COLOR=#0000FF]<[/color][COLOR=#800000]title[/color][COLOR=#0000FF]>[/color][COLOR=#000000]Upload Files[/color][COLOR=#0000FF]</[/color][COLOR=#800000]title[/color][COLOR=#0000FF]>[/color][COLOR=#000000]
[/color][COLOR=#0000FF]</[/color][COLOR=#800000]head[/color][COLOR=#0000FF]>[/color][COLOR=#000000]
[/color][COLOR=#0000FF]<[/color][COLOR=#800000]body[/color][COLOR=#0000FF]>[/color][COLOR=#000000]
    [/color][COLOR=#0000FF]<[/color][COLOR=#800000]form[/color][COLOR=#000000] [/color][COLOR=#FF0000]id[/color][COLOR=#0000FF]="form1"[/color][COLOR=#000000] [/color][COLOR=#FF0000]runat[/color][COLOR=#0000FF]="server">[/color][COLOR=#000000]
    [/color][COLOR=#0000FF]<[/color][COLOR=#800000]div[/color][COLOR=#0000FF]>[/color][COLOR=#000000]
        [/color][COLOR=#0000FF]<[/color][COLOR=#800000]asp[/color][COLOR=#0000FF]:[/color][COLOR=#800000]FileUpload[/color][COLOR=#000000] [/color][COLOR=#FF0000]ID[/color][COLOR=#0000FF]="FileUpload1"[/color][COLOR=#000000] [/color][COLOR=#FF0000]runat[/color][COLOR=#0000FF]="server"[/color][COLOR=#000000] [/color][COLOR=#0000FF]/><[/color][COLOR=#800000]br[/color][COLOR=#000000] [/color][COLOR=#0000FF]/>[/color][COLOR=#000000]
        [/color][COLOR=#0000FF]<[/color][COLOR=#800000]br[/color][COLOR=#000000] [/color][COLOR=#0000FF]/>[/color][COLOR=#000000]
        [/color][COLOR=#0000FF]<[/color][COLOR=#800000]asp[/color][COLOR=#0000FF]:[/color][COLOR=#800000]Button[/color][COLOR=#000000] [/color][COLOR=#FF0000]ID[/color][COLOR=#0000FF]="Button1"[/color][COLOR=#000000] [/color][COLOR=#FF0000]runat[/color][COLOR=#0000FF]="server"[/color][COLOR=#000000] [/color][COLOR=#FF0000]OnClick[/color][COLOR=#0000FF]="Button1_Click"[/color][COLOR=#000000] 
         [/color][COLOR=#FF0000]Text[/color][COLOR=#0000FF]="Upload File"[/color][COLOR=#000000] [/color][COLOR=#0000FF]/>[/color][COLOR=#FF0000]&nbsp;[/color][COLOR=#0000FF]<[/color][COLOR=#800000]br[/color][COLOR=#000000] [/color][COLOR=#0000FF]/>[/color][COLOR=#000000]
        [/color][COLOR=#0000FF]<[/color][COLOR=#800000]br[/color][COLOR=#000000] [/color][COLOR=#0000FF]/>[/color][COLOR=#000000]
        [/color][COLOR=#0000FF]<[/color][COLOR=#800000]asp[/color][COLOR=#0000FF]:[/color][COLOR=#800000]Label[/color][COLOR=#000000] [/color][COLOR=#FF0000]ID[/color][COLOR=#0000FF]="Label1"[/color][COLOR=#000000] [/color][COLOR=#FF0000]runat[/color][COLOR=#0000FF]="server"></[/color][COLOR=#800000]asp[/color][COLOR=#0000FF]:[/color][COLOR=#800000]Label[/color][COLOR=#0000FF]></[/color][COLOR=#800000]div[/color][COLOR=#0000FF]>[/color][COLOR=#000000]
    [/color][COLOR=#0000FF]</[/color][COLOR=#800000]form[/color][COLOR=#0000FF]>[/color][COLOR=#000000]
[/color][COLOR=#0000FF]</[/color][COLOR=#800000]body[/color][COLOR=#0000FF]>[/color][COLOR=#000000]
[/color][COLOR=#0000FF]</[/color][COLOR=#800000]html[/color][COLOR=#0000FF]>[/color]
 
I used something like you suggested Moregelen.
FileUpload fileUpload = (FileUpload)controlCell;
HttpPostedFile myFile = fileUpload.PostedFile;
int nFileLen = myFile.ContentLength;
byte[] myData = new byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);


But I still doesn't understand why the previous code did not work.
( OK for the rights issue for files in 'myDocuments' - but when I run the app on the web, I get an error on any files ...)
I suppose the most important is that I managed to do what I needed to ...
Thank you for taking the time to answer me !

 
The reason you got an error is that you need to remember that this code is running on the server as the server. It has absolutely no access to the connecting machines files unless the user explicitly gives access (either through trippy ActiveX mechanisms or through a file upload). Even if you are running the website locally, the IIS service still will only have access to its own folders; it doesn't get to run wild on your system.

Just remember.. while this is written like a program, ALL of the processing happens on the server end. By the time it reaches the client, it isn't anything more than an HTML page.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top