Reputation: 1432
I need to store product images foreach product. I decided that it is a good way to store images in a folder and save url's for them in database table. Now table looks like:
ProductId|Name |ImgUrl |
-----------------------
1 |Prod1|SomeUrl|
But where shuld I save images? In Conten/Image folder? And how then get the path to this folder?
Upvotes: 0
Views: 2052
Reputation: 9131
You can save it on Content folder, I usually save it on Content folder for maintenance so that if some other programmers look for particular images, they can easily check it on Content folder:
string strPath = Server.MapPath(Url.Content("~/Content/MyImageFolder/")) + "filename.jpg";
file.SaveAs(strPath); //HttpPostedFileBase file
To access it you can do this:
string strDirectory = Server.MapPath(Url.Content("~/Content/MyImageFolder/"));
string[] strFiles = Directory.GetFiles(strDirectory);
string strFileName = string.Empty;
foreach (var strFile in strFiles)
{
strFileName = Path.GetFileName(strFile);
}
Upvotes: 2