【问题标题】:Best way to store images in Asp.Net MVC 5在 Asp.Net MVC 5 中存储图像的最佳方式
【发布时间】:2017-09-19 14:14:15
【问题描述】:

我需要为每个产品存储产品图像。 我认为这是将图像存储在文件夹中并将它们的 url 保存在数据库表中的好方法。 现在表格看起来像:

ProductId|Name |ImgUrl |
-----------------------
1        |Prod1|SomeUrl|

但是我应该在哪里保存图像? 在内容/图像文件夹中? 那么如何获取这个文件夹的路径呢?

【问题讨论】:

  • 多少张图片?多大?安全和备份怎么样?

标签: c# asp.net asp.net-mvc


【解决方案1】:

你可以将它保存在Content文件夹中,我通常将它保存在Content文件夹中进行维护,这样如果其他程序员寻找特定的图像,他们可以轻松地在Content中查看strong> 文件夹:

string strPath = Server.MapPath(Url.Content("~/Content/MyImageFolder/")) + "filename.jpg";

file.SaveAs(strPath); //HttpPostedFileBase file

要访问它,您可以这样做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 2011-06-04
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多