简体   繁体   English

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

[英]How to use local drive path in ASP.NET MVC4

I have saved File in local drive and now stuck with fetching it back to screen. 我已经将文件保存在本地驱动器中,现在卡住了,将其取回屏幕。

Scenario- 方案-

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

As an example I saved file- Desert.png into this directory location. 例如,我将文件Desert.png保存到此目录位置。

So database CardFilePath column is updated this this path- 因此,数据库CardFilePath列将通过以下路径更新:

D:\\FunRanger2\\FunRangerPhotos\\Desert.png

On view side- 从侧面看

I check that this path is coming in model value- 我检查此路径是否进入模型值-

@Model.cardFilePath is D:\\FunRanger2\\FunRangerPhotos\\Desert.png @Model.cardFilePathD:\\FunRanger2\\FunRangerPhotos\\Desert.png

Trying to get it in image- 试图以图像形式获得它

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

All I get is no Image there. 我所得到的只是那里没有图像。

How Can I get this image on viewpage? 如何在查看页上获取此图像?

You need to put that image relative to your site so it can be served. 您需要将该图片相对于您的网站放置,以便将其投放。 For example, if you place the image in the root of your website the path would just be the image name. 例如,如果将图像放置在网站的根目录中,则路径将仅是图像名称。 If you put it in a folder named images it would be something like /images/{image_name} . 如果将其放在名为images的文件夹中,则类似于/images/{image_name}

Think about what the browser is doing. 考虑一下浏览器在做什么。 It's trying to access a file at D:\\FunRanger2\\FunRangerPhotos on the client machine. 它正在尝试访问客户端计算机D:\\FunRanger2\\FunRangerPhotos的文件

You must create an action with File result: instead of returning a view, return a file like this: 您必须使用File结果创建一个动作:返回以下文件而不是返回视图:

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

This overload have two parameters. 此重载有两个参数。 The first one is the path to the file. 第一个是文件的路径。 The seocond one, is the MIME type of your file, which is related to the file type/extension, and you can easily find a lis of this types, for example, here . 第二个是文件的MIME类型,它与文件类型/扩展名有关,例如, 在此处 ,您可以轻松找到这种类型的lis。

Then you simply have to point the src attribute to this, for example with Url.Action() : 然后,您只需要将src属性指向此位置,例如使用Url.Action()

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

You'd rather use an id in this action, instead of whowing the server path. 您宁愿在此操作中使用id,而不是使用服务器路径。 For example: 例如:

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

Use it in your Razor: 在剃刀中使用它:

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

You can map the folder as a virtual directory in IIS. 您可以将文件夹映射为IIS中的虚拟目录。 First Store the files in D Drive as you are doing. 首先,将文件存储在D驱动器中。 Map the location of D Drive Images as a Virtual Directory (Simply right click the website node in IIS and select Add Virtual Directory) in the Website as shown below. 如下图所示,将D驱动器映像的位置映射为虚拟目录(只需在IIS中右键单击网站节点,然后选择“添加虚拟目录”)。

This will make all the D Drive location accessible using relative path. 这样可以使用相对路径访问所有D驱动器位置。

在此处输入图片说明

Once you add it as Virtual Directory, you can use it as following in your code. 将其添加为虚拟目录后,就可以在代码中按以下方式使用它。

<img src='~/Images/.....your image name...' />

you can read the image as bytearray and return it to the view 您可以将图像读取为字节数组并将其返回到视图

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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