cagin
cagin

Reputation: 5930

FileUpload does not upload file

I want to upload an image to file but there is an exception as "The SaveAs method is configured to require a rooted path, and the path '../TempCharcoal/IMG_0153.JPG' is not rooted."

Here is my simple code:

if (fuImage.HasFile)
  {
     fuImage.SaveAs("../TempCharcoal/IMG_0153.JPG");
  }

I want to upload it in CharcoalForm.aspx to TempCharcoal folder. You can see that they are in the same root but I don't know what is wrong.

enter image description here

Upvotes: 1

Views: 207

Answers (2)

SShaheen
SShaheen

Reputation: 1013

Use this instead:

if (fuImage.HasFile)
  {
     fuImage.SaveAs(Server.MapPath(@"~/TempCharcoal/IMG_0153.JPG"));
  }

The file path that you have is a relative path (relative to whatever directory .. is). The MapPath method will return the physical file path that corresponds to that path.

Upvotes: 2

Ratan
Ratan

Reputation: 883

Try using:

string myPath = @"~\TempCharcoal\IMG_0153.jpg";
fuImage.SaveAs(Server.MapPath(myPath));

Upvotes: 0

Related Questions