【问题标题】:How to use local drive path in ASP.NET MVC4如何在 ASP.NET MVC4 中使用本地驱动器路径
【发布时间】:2014-01-24 22:42:28
【问题描述】:

我已将文件保存在本地驱动器中,现在坚持将其取回屏幕。

场景-

 public ActionResult Upload(HttpPostedFileBase file, CardModel card) {
        CardTable cardtable = new CardTable();
        if (file != null && file.ContentLength > 0) {
            // TODO: storing uploaded files to the App_Data folder on the server. 
            // Adjust this location to fit your requirements
            var drivepath = "D:\\FunRanger2\\FunRangerPhotos";
            var filepath = Path.Combine(drivepath, Path.GetFileName(file.FileName));
            file.SaveAs(filepath);
            cardtable.CardFileName = file.FileName;
            cardtable.CardFilePath = filepath;
            cardtable.CardDate = DateTime.Now;
            cardtable.CardTitle = card.cardTitle;
            cardtable.CardHashCode = card.cardHashCode == null ? "" : card.cardHashCode;
            db.CardTables.InsertOnSubmit(cardtable);
            db.SubmitChanges();
        }

例如,我将文件 - Desert.png 保存到此目录位置。

所以数据库CardFilePath 列更新到这个路径-

D:\FunRanger2\FunRangerPhotos\Desert.png

在视图方面-

我检查这条路径是否以模型值出现-

@Model.cardFilePathD:\FunRanger2\FunRangerPhotos\Desert.png

尝试在图像中获取它-

 <img src="@Model.cardFilePath" height="300" width="300"  />

我得到的只是那里没有图像。

如何在视图页面上获取此图像?

【问题讨论】:

  • 如果您想将其保存在解决方案中的文件夹以外的其他文件夹中,请尝试像这样处理它们(放置 file:/// 前缀):file:///D:\FunRanger2\FunRangerPhotos\Desert .png

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


【解决方案1】:

您需要将该图片相对于您的网站进行投放,以便投放。例如,如果您将图像放在网站的根目录中,则路径就是图像名称。如果你把它放在一个名为 images 的文件夹中,它会类似于 /images/{image_name}

想想浏览器在做什么。它正在尝试访问 客户端计算机上D:\FunRanger2\FunRangerPhotos 的文件。

【讨论】:

  • 不,我更喜欢将其保存在本地驱动器中。有没有其他方法只能从本地驱动器映射它?
  • @user3163213:根本没有——浏览器无法像那样访问本地驱动器也不应该这样做。跨度>
  • 如果你真的想假设你网站的所有用户在他们的驱动器上都有相同的文件夹结构,你很确定这就是你想要的,只需在客户端使用javascript。但是客户端-服务器应用程序不是这样工作的。
  • @BashirMagomedov,试一试以确保它是否有效。
【解决方案2】:

你必须创建一个带有File结果的动作:而不是返回一个视图,而是返回一个像这样的文件:

return File("FileName", "img/png");

这个重载有两个参数。第一个是文件的路径。第二个,是你文件的MIME类型,和文件类型/扩展名有关,你可以很容易地找到这个类型的列表,例如here

然后您只需将src 属性指向此,例如Url.Action()

<img src="@Url.Action(...)" height="300" width="300"  />

您宁愿在此操作中使用 id,而不是使用服务器路径。例如:

public Image(int id)
{
    // get the filePath using the id:
    string filePath = ...;
    // get the mime type from the file extension
    string mimeType;
    return File(filePath, mimeType);
}

在你的剃须刀中使用它:

<img src="@Url.Action("Image", "ControllerName", new { id = ...})" 
    height="300" width="300"  />

【讨论】:

    【解决方案3】:

    您可以将该文件夹映射为 IIS 中的虚拟目录。首先将文件存储在 D Drive 中。将D盘镜像的位置映射为网站中的虚拟目录(只需在IIS中右键点击网站节点,选择添加虚拟目录即可),如下图所示。

    这将使所有 D 驱动器位置都可以使用相对路径访问。

    将其添加为虚拟目录后,您可以在代码中使用它。

    &lt;img src='~/Images/.....your image name...' /&gt;

    【讨论】:

      【解决方案4】:

      您可以将图像读取为字节数组并将其返回给视图

      public ActionResult Picture()
              {
                  Byte[] Picture;
      
                  var filePath = "D:\FunRanger2\FunRangerPhotos\Desert.png";
                  if (System.IO.File.Exists(filePath))
                  {
                      Picture = System.IO.File.ReadAllBytes(filePath);
                  }
      
                  return File(Picture, "image/png");
              }
      

      【讨论】:

        猜你喜欢
        • 2013-07-13
        • 2013-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多