从App_data文件夹访问图像

时间:2012-01-02 14:37:41

标签: asp.net

我想在app_data文件夹中存储一些图像,我知道它是一个安全的文件夹 但我不知道如何从HTML访问此图像文件(无图像显示)

是否有任何建议,或者我如何将图像存储在无法从外部用户访问的安全位置

3 个答案:

答案 0 :(得分:1)

App_Data文件夹旨在保存数据库,XML文件和其他形式的数据存储。没有必要在那里存储您的图像。而是在其他位置创建一个文件夹,并使用您的Web服务器禁用该文件夹上的目录浏览。最后,如果用户可以看到图像,他们可以以某种方式访问​​它,但禁用目录浏览器意味着他们不能像在Windows资源管理器中那样遍历文件夹。

答案 1 :(得分:1)

以下内容可以帮助您入门。

namespace Moo.Core.Handlers
{
    public class Thumbnail : IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            String FileName = Path.GetFileName(context.Request.PhysicalPath).Replace(".th", "");
            String AssetName = HttpContext.Current.Request.MapPath(Path.Combine(HttpContext.Current.Request.ApplicationPath, "UserData/Images/" + FileName));

            if (File.Exists(AssetName))
            {
                context.Response.ContentType = ImageManipulation.TypeOf(AssetName).MimeType;
                context.Response.BinaryWrite(File.ReadAllBytes(AssetName));
                context.Response.End();
            }
        }
    }
}

答案 2 :(得分:-1)

如果您只想控制对图像的访问,您可以将文件存储在数据库中,并将其作为二进制图像提供给您想要的用户。例如:

<img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />

您可以详细了解here

相关问题