图像无法跨域访问

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>
相关推荐
计算机安禾3 小时前
【数据库系统原理】第19篇:计算机存储层次结构与数据库文件的物理组织
数据库·oracle
JAVA面经实录9174 小时前
操作系统面试题
java·服务器·数据库·计算机网络·面试
摇滚侠4 小时前
mariadb-libs 被 mysql-community-libs-5.7.28-1.el7.x86_64 取代
数据库·mysql·mariadb
DIY源码阁5 小时前
JavaSwing饮品管理系统 - MySQL版
java·数据库·mysql·eclipse
专注搞钱5 小时前
GPT-4o写设备Recipe:从3小时到10分钟
数据库·人工智能·gpt·半导体
东风破1376 小时前
达梦数据库实战:备份恢复与数据迁移全攻略(实例初始化、服务注册、路径迁移)
数据库·chrome
SelectDB技术团队6 小时前
2026 SelectDB AI 产品发布会:Agent Native 数据基础设施能力全景发布
数据库·人工智能·agent·apache doris·selectdb
爱吃羊的老虎6 小时前
【数据库】模块一:数据库基础与关系代数
数据库
dishugj7 小时前
iSCSI + Multipath + ASM:Oracle RAC 共享存储技术链详解
数据库·oracle
yoothey7 小时前
MySQL事务机制解析 - 面试高分知识点
数据库·mysql·面试