Reputation:
I know that this question was asked many times here before and I read it , but I can't find a solution to my problem. I'm trying to save image to the folder in my project : here is my code behind :
if (FileUpload_UploadFile.HasFile)
{
int contentLength = FileUpload_UploadFile.PostedFile.ContentLength;
string contentType = FileUpload_UploadFile.PostedFile.ContentType;
string fileName = FileUpload_UploadFile.PostedFile.FileName;
string imgPath = "~/Image/" + fileName ;
FileUpload_UploadFile.PostedFile.SaveAs(Server.MapPath(imgPath));
}
and this is the stack trace :
[DirectoryNotFoundException: Could not find a part of the path 'C:\Users\.bimanathan\Desktop\PETA\PETA\Image\0eaf985f79f0ceff94aa8c585a31f745_650x.jpg'.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +359
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) +1305
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
PETA.CreateNews.btnSubmit_Click(Object sender, EventArgs e) in c:\Users\.bimanathan\Desktop\PETA\PETA\TEST\CreateNews.aspx.cs:73
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628026
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) +1724
I don't know how to fix it please help me. I am beginner in c#
Upvotes: 0
Views: 3240
Reputation: 149
To make sure the path is constructed in a correct way I always use Path.Combine(String, String)
System.IO.Path.Combine("/Image/", fileName);
If you use web,you can use VirtualPathUtility(String, String)
System.Web.VirtualPathUtility.Combine("~/Image/", fileName)
Upvotes: 0
Reputation: 2514
Drop the tilde from the /image/ path. Server.MapPath does not use it. eg:
string imgPath = "/Image/" + fileName ;
See here for a table of relative expressions for paths: https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath%28v=vs.110%29.aspx
The user running the www service needs to have write permissions to the location in question. often this user is a service, not an interactive user. Confirm that the service user has appropriate read and write permissions.
Also, be particularly careful about allowing users to upload documents into the site directory hierarchy itself. It requires that your default permissions allow write to your site root, which can be misused in many ways.
Upvotes: 1