1

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

cagin
  • 5,772
  • 14
  • 74
  • 130

2 Answers2

2

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.

SShaheen
  • 1,012
  • 7
  • 21
0

Try using:

string myPath = @"~\TempCharcoal\IMG_0153.jpg";
fuImage.SaveAs(Server.MapPath(myPath));
Ratan
  • 863
  • 5
  • 12
  • 28