Gleb
Gleb

Reputation: 1432

Best way to store images in Asp.Net MVC 5

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

Answers (1)

Willy David Jr
Willy David Jr

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

Related Questions