Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET MVC4中使用本地驱动器路径_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 如何在ASP.NET MVC4中使用本地驱动器路径

C# 如何在ASP.NET MVC4中使用本地驱动器路径,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我已将文件保存在本地驱动器中,现在仍坚持将其恢复到屏幕 情景- 例如,我将文件-Desert.png保存到这个目录位置 因此,数据库CardFilePath列将更新到此路径- D:\FunRanger2\FunRangerPhotos\Desert.png 在视图侧- 我检查此路径是否为模型值- @Model.cardFilePath是D:\FunRanger2\FunRangerPhotos\Desert.png 试图把它形象化- 我所得到的只是那里没有图像 如何在viewpage上获取此图像

我已将文件保存在本地驱动器中,现在仍坚持将其恢复到屏幕

情景-

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

因此,数据库CardFilePath列将更新到此路径-

D:\FunRanger2\FunRangerPhotos\Desert.png

在视图侧-

我检查此路径是否为模型值-

@Model.cardFilePath是D:\FunRanger2\FunRangerPhotos\Desert.png

试图把它形象化-

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


如何在viewpage上获取此图像?

您需要将该图像与您的站点相对放置,以便为其提供服务。例如,如果您将图像放在网站的根目录中,则路径仅为图像名称。如果你把它放在一个名为images的文件夹中,它将类似于/images/{image\u name}


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

您必须使用文件结果创建一个操作:而不是返回视图,返回如下文件:

return File("FileName", "img/png");
此重载有两个参数。第一个是文件的路径。seocond是文件的MIME类型,它与文件类型/扩展名相关,您可以很容易地找到这种类型的lis,例如

然后,您只需将src属性指向它,例如Url.Action:

在剃须刀中使用:

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

您可以将文件夹映射为IIS中的虚拟目录。首先,按照您的操作将文件存储在D驱动器中。将D驱动器映像的位置映射为虚拟目录,只需右键单击IIS中的网站节点,然后选择在网站中添加虚拟目录,如下所示

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

将其添加为虚拟目录后,可以在代码中使用它,如下所示


您可以将图像读取为bytearray并将其返回到视图

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

如果要将其保存在除解决方案中的文件夹之外的其他文件夹中,请尝试按如下方式对其进行寻址:put file:///前缀:file:///D:\FunRanger2\FunRangerPhotos\Desert.pngNo,我更喜欢将其保存在本地驱动器中。有没有其他方法可以只从本地驱动器映射它?@user3163213:一点也没有-浏览器没有访问本地驱动器的权限,也不应该这样。如果你真的想假设你网站的所有用户在他们的驱动器上都有相同的文件夹结构,并且你很确定这就是你想要的,那么就在他们的驱动器上进行吧客户端使用javascript。“但客户机-服务器应用程序不是这样工作的。@BashirMagomedov,试着确认它是否工作。
<img src="@Url.Action(...)" height="300" width="300"  />
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"  />
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");
        }