Reputation: 533
I'm trying to save a banner picture and before saving to a folder I'm checking and creating the respective folder as well but it gives me error even though I checked that the folder exists. Here is the code:
HttpPostedFileBase banner = Request.Files["banner"];
if (banner != null && banner.ContentLength > 0) {
var folder = Server.MapPath("~/images/Continents/");
if (!Directory.Exists(folder)) {
Directory.CreateDirectory(folder);
}
banner.SaveAs(Server.MapPath("~/images/Continents/" + image.FileName));
string x = "/images/Continents/" + image.FileName;
continent.BANNER = x;
}
Is there something I'm missing?
Upvotes: 1
Views: 2761
Reputation: 3520
To concatenate Paths in Server.MapPath
, use Path.Combine()
as follow:
banner.SaveAs(Server.MapPath(Path.Combine("~/images/Continents", image.FileName)));
Upvotes: 1