emilios
emilios

Reputation: 375

file upload C# asp.net

i am trying to upload files in asp.net and putting a specific path to be saved. windows user.

it is outputting me an error :

System.UnauthorizedAccessException was unhandled by user code
  Message=Access to the path 'C:\Users\USER\Desktop\fyp2\CMS TEST4\CMS\CMS\Upload' is denied. 

my code is:

 var guid = Guid.NewGuid();
 if (File.HasFile)
 {
   var length = File.PostedFile.FileName.ToString().Length;
   var ind = File.PostedFile.FileName.ToString().IndexOf('.');
   var sdfs=guid.ToString()+File.PostedFile.FileName.ToString().Substring(ind, length - ind);
   File.PostedFile.SaveAs("C:\\Users\\USER\\Desktop\\fyp2\\CMS TEST4\\CMS\\CMS\\Upload");

 }

Upvotes: 1

Views: 1394

Answers (4)

Shairon Toledo
Shairon Toledo

Reputation: 2124

Looks like current user has not permission to save. Before writing try use FileSystemRights with AccessControlType.Allow to know permission info of the destination.

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160982

The ASP.NET worker process does not have access rights to that path. By default I believe IIS worker processes run under the Network service account. You can either add write rights for the folder to this account, or set up a new Application Pool with a different identity (i.e. a user that does have write rights).

Upvotes: 3

user610217
user610217

Reputation:

The users directory is fairly locked down. If the account the web server is running under is not the specified user, it will not, by default, have access to the path. You can either grant explicit access to that path to the account running your web server, or create a folder with appropriate permissions external to that path and create a link on the user's desktop.

Upvotes: 3

As simple as the error puts it, your application requires the folder to have proper write permissions.

I'm asuming this is a Web Application. In that case, you'll need that the user which IIS uses to run applications has Write permissions over the specified folder.

Upvotes: 3

Related Questions