Click here to Skip to main content
15,881,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to upload user image on website. Here I'm using FileUpload control to upload image.

My Code is like this

C#
string fileName = string.Empty;
if (fuFranchiseImage.HasFile == true)
{
    fileName = DateTime.Now.ToString("MM-dd-yyyy_HHmmss");
    string fileExtension = System.IO.Path.GetExtension(this.fuFranchiseImage.FileName).ToString();
    fileName = fileName + fileExtension;
    string filePath = Server.MapPath(@"~\images\" + fileName);
    fuFranchiseImage.SaveAs(filePath);
}

Here i'm trying to save images into images folder in the application, and in SQL Server i just save file name.

When I try to upload in server its getting error like this

Access to the path 'D:\Hosting\12374540\html\report\images\04-10-2014_000650.jpg' is denied.


Please could anyone help me to solve this problem
Posted

There is no issue in code. You have to set access rights(Permission) for images directory on server. Currently you don't have access to save image on server.
 
Share this answer
 
Comments
Aboobakkar Siddeq D U 10-Apr-14 3:31am    
How to check for folder permission? I went through FileZilla and right button clicked on images folder, then clicked on Properties then I saw
1. Owner Permissions
2. Group Permissions
3. Public Permissions
I checked all options but still getting same problem.

Is there other changes to do in that?
Member 13794985 6-Feb-19 6:46am    
Hi #Aboobakkar Siddeq D U ... How did you solved this issue.. I have been trying to fix this from the last 1day.. Mine is also same issue.. Fileupload is working in local but not in server. I couldn't find any errors like permission denied but observed that server.mappath is returning the path with full path from the root like C:// or D://. How can I save uploaded file in server within the project without giving full path..
Check your folder permissions: you probably need write access to all users.
 
Share this answer
 
Comments
Aboobakkar Siddeq D U 10-Apr-14 3:31am    
How to check for folder permission? I went through FileZilla and right button clicked on images folder, then clicked on Properties then I saw
1. Owner Permissions
2. Group Permissions
3. Public Permissions
I checked all options but still getting same problem.

Is there other changes to do in that?
Ankur\m/ 10-Apr-14 4:11am    
All users? Just the account under which ASP.NET application is running needs write permission to that folder.
I think it's wrong with your
string filePath = Server.MapPath(@"~\images\" + fileName);
I think you shall loose the ~
I write:
string filePath = Server.MapPath(@"\images\" + fileName);
or
string filePath = Server.MapPath("~\images\" + fileName);
 
Share this answer
 
v2
Comments
Aboobakkar Siddeq D U 10-Apr-14 3:32am    
Let me check for that........
Aboobakkar Siddeq D U 10-Apr-14 7:09am    
No....still its getting same error
The process under which you web application is running does not have write access to the directory. That is the default setting for default account under which asp.net run. You options are - either you can use a more privileged user account (which isn't a very good idea) or give write permission to that folder for that a/c. You can find ways to do that here - https://www.google.co.in/#q=file+upload+permission+asp.net[^]
On more searching you can find an article on MSDN explaining this in detail.

Hope that helps!
 
Share this answer
 
Comments
Aboobakkar Siddeq D U 10-Apr-14 5:03am    
NOW I AM GETTING THIS ERROR

Server Error in '/report' Application.

Security Exception

Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Source Error:


[No relevant source lines]

Source File: App_Web_ame2uqid.0.cs Line: 0

Stack Trace:


[SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark) +31
System.Security.CodeAccessPermission.Demand() +46
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +654
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +60
System.IO.FileStream..ctor(String path, FileMode mode) +55
System.Web.HttpPostedFile.SaveAs(String filename) +94
System.Web.UI.WebControls.FileUpload.SaveAs(String filename) +23
ADMIXS.AdminPanel.AddFranchisee.btnSubmit_Click(Object sender, EventArgs e) in AddFranchisee.aspx.cs:134
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9556538
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6704
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +245
System.Web.UI.Page.ProcessRequest() +72
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
System.Web.UI.Page.ProcessRequest(HttpContext context) +58
ASP.adminpanel_addfranchisee_aspx.ProcessRequest(HttpContext context) in App_Web_ame2uqid.0.cs:0
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +341
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18067
Ankur\m/ 10-Apr-14 6:46am    
The issue is related to trust level of your website. You may need to contact your host provider. Check this link for more details - http://forums.asp.net/t/1422162.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900