如何在页面上显示图像时,图像存储Web根目录以外 [英] How to display images on a page when images are stored outside of web root

查看:143
本文介绍了如何在页面上显示图像时,图像存储Web根目录以外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户上载图像时,它被存储在文件系统,但外面的是什么公共可到达的。

When users upload an image, it is stored in the file system but outside of what is publicly reachable.

我显示项目的列表,并且每个项目具有与其相关联的图像。

I am displaying a list of items, and each item has an image associated with it.

我怎样才能显示图像时,实际的文件存储在wwwroot文件夹之外吗?即它是不公开的。

How can I display the image when the actual file is stored outside of the wwwroot folder? i.e. it isn't publicly available.

推荐答案

由于该操作方法是在服务器上运行,它可以访问它有权限的任何文件。本文件并不需要是的wwwroot 文件夹中。你只需要告诉操作方法来获得它的形象。

Since the action method is running on the server, it can access any file it has permission to. The file does not need to be within the wwwroot folder. You simply need to tell the action method which image to get.

例如:

<img src="/mycontroller/myaction/123">

然后,您的行动将是这样的:

Your action would then look like:

public FileResult MyAction(int id)
{
    var dir = "c:\myimages\";
    var path = Path.Combine(dir, id + ".jpg");

    return new FileStreamResult(new FileStream(path, FileMode.Open), "image/jpeg");
}

请注意,该ID是 INT 将prevent有人从注入路径来访问的驱动器/股不同的文件。

Please note that the id is an int which will prevent someone from injecting a path to access different files on the drive/share.

这篇关于如何在页面上显示图像时,图像存储Web根目录以外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆