禁用对图像的直接访问 [英] Disable direct access to images

查看:133
本文介绍了禁用对图像的直接访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个小的家庭相册,并打算也许是开给其他人后存储图像。

I am making a little family photo album, with the intention to maybe open it to other people to store images later.

我上传的图片来〜\\ \\图像,然后调整他们的3倍(普通视图...缩略图和一个小的版本),并将其移动到〜\\影像\\竖起大拇指,〜\\影像\\正常的,〜\\影像\\微小然后将原来的以〜\\影像\\原件。

I upload the images to ~\images\, then resize them 3 times (Normal view ... thumbnail and a tiny version) and move them to ~\images\thumbs, ~\images\normal, ~\images\tiny and then move the original to ~\images\original.

如果用户知道文件名,他们可以转到 http://mysite.com/images/normal/filename .JPG 直接访问。

If a user knows a file name, they can just goto http://mysite.com/images/normal/filename.jpg for direct access.

我preFER是不提供给他们。

I'd prefer that not to be available to them.

那么,有没有办法改变的ASP的ImageUrl:图像控制,而从非accessable文件夹看?而这会不会影响性能?我想是这样(不知道这是可能的)读取图像到s的蒸​​汽,并以某种方式设置的ImageUrl或什么都从流中读取?

So, is there a way to change the ImageUrl of the asp:Image control, to rather read from a non-accessable folder? And would this be a performance hit? I'm thinking something like (Not sure if it's possible) reading the image into s Steam, and somehow setting the ImageUrl or what ever, to read from the stream?

希望你能帮助。

推荐答案

答案是肯定的。可以使用IMG SRC的一个特殊格式将直接在页中嵌入图像。

The answer is yes. You can use a special format of img src which will embed the image directly in the page.

相反的URL,可以使用以下格式:

Instead of the URL, you would use the following format:

<img src="data:image/png;base64,..." />

在哪里数据指定图像的MIME类型和... BASE64后,以base64恩的形象$ C $光盘内容。

where data specifies the mime type of your image and the ... after base64, is the base64 encoded contents of the image.

此行​​为是由该RFC定义

下面是一个例子:

在HTML:

<img id="MyPicture" runat="server" border="0" />

在code-背后:

    protected void Page_Load(object sender, EventArgs e)
    {
        MyPicture.Src = @"data:image/gif;base64," + EncodeFile(Server.MapPath(@"/images/test.gif"));
    }

    private string EncodeFile(string fileName)
    {
        return Convert.ToBase64String(File.ReadAllBytes(fileName));
    }

这篇关于禁用对图像的直接访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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