图像无法跨域访问

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>
相关推荐
Freedom℡1 小时前
Spark,连接MySQL数据库,添加数据,读取数据
数据库·hadoop·spark
Code哈哈笑2 小时前
【图书管理系统】用户注册系统实现详解
数据库·spring boot·后端·mybatis
2401_837088502 小时前
SQL性能分析
数据库·sql
瓜皮弟子头很铁2 小时前
多项目共用SQL 添加需要字段
数据库·sql
CryptoRzz2 小时前
股票数据源对接技术指南:印度尼西亚、印度、韩国
数据库·python·金融·数据分析·区块链
Pluto_CSND2 小时前
hbase shell的常用命令
大数据·数据库·hbase
哈哈真棒3 小时前
sparkSQL读入csv文件写入mysql(2)
数据库·mysql
Cynicism_Smile3 小时前
Mysql 8.0.32 union all 创建视图后中文模糊查询失效
数据库·mysql
小oo呆3 小时前
【自然语言处理与大模型】向量数据库技术
数据库·人工智能·自然语言处理
Aurora_NeAr3 小时前
Redis设计与实现——Redis命令参考与高级特性
数据库·redis·缓存