Reputation: 742
Hello i am currently able to save images to my server using this function:
HttpPostedFile filePosted = CategoryImage.PostedFile;
string fullPath = "";
if (filePosted != null && filePosted.ContentLength > 0)
{
int contentLength = CategoryImage.PostedFile.ContentLength;
string contentType = CategoryImage.PostedFile.ContentType;
string filename = filePosted.FileName;
string fileextension = Path.GetExtension(filename);
fullPath = Server.MapPath(Path.Combine("~/img/logos/", filename));
if (fileextension == ".jpg" || fileextension == ".jpeg" || fileextension == ".png" || fileextension == ".bmp")
{
CategoryImage.PostedFile.SaveAs(fullPath);
}
}
But the image path in my database gets saved as this:
D:\Projects\*************\****\img\logos\image1.png
Now when i assign this path to an html img "src" attribute the image does not display. i get no errors when running my code, preferrably i would like the path to be something like this:
~/******/img/image1.png
The image displays when i use the format above.
Upvotes: 0
Views: 56
Reputation: 907
You can substitute the full path by the relative one and insert it in the src attribute.
string s1 = @"D:\Projects\*************\****\img\logos\image1.png";
string s2 = "~/****/" + s1.Substring(s1.IndexOf("img")).Replace("\\", "/");
// use s2 as src attribute for img
Upvotes: 1
Reputation: 284
Server.MapPath("~") returns the physical path to the root of the application
Have you tried using just fullPath = Server.MapPath(Path.Combine("/img/logos/", filename))
Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)
I think with "/" this will place your images in a folder that IIS can access rather then the D: folder
Upvotes: 0