图像无法跨域访问

Access to image at 'https://xd5g.oss-cn-guangzhou.aliyuncs.com/f26500ad6e884e6da1690f164b81fb88.png?Expires=1731642871\&OSSAccessKeyId=LTAI5tKYesUMvXUm2FRCuA52\&Signature=ragYN%2BP7CKNDzWXMlqGEtUBikLU%3D' from origin 'https://in.xd5g.cn' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.


1、网页站点下添加,页面 Img.cshtml (用于图像数据转发)

cs 复制代码
@using System.Drawing;
@using System.Web;
@{
    var src = Request["src"];
    Response.Clear();
    try
    {
        Bitmap img = GetWebPic(src);
        OutPutPic(img, DateTime.Now.Ticks + "", Response);
    }
    catch { }
    Response.Write("");
}

@functions  {

    /// <summary>
    /// 输出图像
    /// </summary>
    /// <param name="pic"></param>
    /// <param name="filename"></param>
    /// <param name="Response"></param>
    private void OutPutPic(Bitmap pic, string filename, HttpResponseBase Response)
    {
        if (pic == null) return;
        System.IO.MemoryStream stream = new System.IO.MemoryStream();
        pic.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        Response.ClearContent();
        Response.ContentType = "Image/png";
        if (!filename.EndsWith(".png")) filename += ".png";
        Response.AddHeader("Content-Disposition", "filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
        Response.BinaryWrite(stream.ToArray());
    }


    /// <summary>
    /// 获取url地址对应的图像
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    private Bitmap GetWebPic(string url)
    {
        try
        {
            WebClient client = new WebClient();
            byte[] data = client.DownloadData(url);
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            stream.Write(data, 0, data.Length);
            Bitmap pic = (Bitmap)Bitmap.FromStream(stream);
            return pic;
        }
        catch (Exception ex)
        {
            return null;
        }
    }
}

2、将图像请求地址,转接到该页面即可。/Img.cshtml?src=图像原有地址XXX


**解决方案二:**站点配置文件中添加

html 复制代码
<system.webServer>
	<staticContent>
		<mimeMap fileExtension=".*" mimeType="application/octet-stream" />
	</staticContent>
	<httpProtocol>
		<customHeaders>
			<add name="Access-Control-Allow-Origin" value="*" />
			<add name="Access-Control-Allow-Headers" value="Content-Type" />
		</customHeaders>
	</httpProtocol>
</system.webServer>
相关推荐
woshilys26 分钟前
sql server index FORCESEEK
数据库·sqlserver
刘艳兵的学习博客1 小时前
刘艳兵-DBA041-使用常用的数据泵功能导出时,主要需要关注以下哪些步骤?
运维·服务器·数据库·sql·mysql·oracle·刘艳兵
刘艳兵的学习博客1 小时前
刘艳兵-DBA044-关于cardinality的描述,正确的是?
运维·数据库·oracle·dba·刘艳兵
小陈phd3 小时前
django从入门到实战(二)——FBV视图介绍
数据库·django·sqlite
架构师Wu老七3 小时前
【软考】系统架构设计师-数据库设计基础
数据库·软考·系统架构设计师
fat house cat_3 小时前
MySQL是怎么解决幻读和不可重复读的?
java·数据库·mysql
自由自在的小Bird3 小时前
高频SQL50题
数据库
前端与小赵3 小时前
什么是‌‌‌‌‌‌SQL,有什么特点
数据库·sql·oracle
吹老师个人app编程教学5 小时前
ClickHouse的介绍、安装、数据类型
数据库·clickhouse·oracle