且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

禁用对图像的直接访问

更新时间:2023-12-01 15:08:46

答案是肯定的。可以使用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));
    }