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>