图像无法跨域访问

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.


 方案一:允许跨域

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

1、在单个页面允许跨域:

2、或在整个站点允许跨域:

<%@ Application Language="C#" %>

<script runat="server">
    
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        // 设置允许跨域的域,*代表允许所有域,也可以设置特定的域
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
    }

    void Application_Start(object sender, EventArgs e)
    {
        // 在应用程序启动时运行的代码

    }

    void Application_End(object sender, EventArgs e)
    {
        //  在应用程序关闭时运行的代码
    }

    void Application_Error(object sender, EventArgs e)
    {
        // 在出现未处理的错误时运行的代码
        Exception exc = Server.GetLastError();
        if (exc is HttpUnhandledException)
        {
            if (exc.InnerException != null)
            {
                Server.Transfer("/errpage/500.aspx?msg="+Server.UrlEncode( exc.InnerException.Message),
                    true);
            }
        }
    }

    void Session_Start(object sender, EventArgs e)
    {
        // 在新会话启动时运行的代码

    }

    void Session_End(object sender, EventArgs e)
    {
        // 在会话结束时运行的代码。 
    }


</script>

3、也可指定允许的域(目标域)

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://xd5g.oss-cn-guangzhou.aliyuncs.com");javascript:void(0)

 方案二:图像转发

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

@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


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

<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>

Chorme浏览器相关:

chrome设置为Disabled(允许外部网站请求本地网址)

 打开chrome的设置: chrome://flags/#block-insecure-private-network-requests

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值