图像无法跨域访问

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>
相关推荐
wyk123_04630 分钟前
Django 连接(sqlserver)数据库方法
数据库·sqlserver·django
李长渊哦6 小时前
使用Druid连接池优化Spring Boot应用中的数据库连接
数据库·spring boot·后端
斗-匕7 小时前
Spring Boot自动装配:约定大于配置的魔法解密
数据库
铁头乔8 小时前
IoTDB 断电后无法启动 DataNode,日志提示 Meet error while starting up
数据库·开源·时序数据库·iotdb
一 乐8 小时前
高校体育场管理系统系统|体育场管理系统小程序设计与实现(源码+数据库+文档)
前端·javascript·数据库·spring boot·高校体育馆系统
来一杯龙舌兰9 小时前
【Postgresql】Windows 部署 Postgresql 数据库 (图文教程)
数据库·windows·postgresql
blog_wanghao9 小时前
C#: 创建Excel文件并在Excel中写入数据库中的数据
数据库·c#·excel
xianwu54310 小时前
反向代理模块kd
开发语言·网络·数据库·c++·mysql
vdigital10 小时前
本地主机(localhost)11434端口 HTTP 连接10061原因及解决
java·服务器·数据库
蓝牙先生10 小时前
git自动化之.netrc配置
数据库