简体   繁体   中英

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?

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);
          }    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM