imagehandler.ashx 图片未显示在 chrome
imagehandler.ashx image doesn't display in chrome
imagehandler.ashx 图片在 Chrome 浏览器中不显示。我该如何修复它..?
我的代码 (imagehandler.ashx):
public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["YazarID"] != null)
{
string YazarID = context.Request.QueryString["YazarID"];
DataTable dt = new DataTable();
string query = "select img from Register where YazarID='" + YazarID + "'";
dt = Database.GetData(query);
HttpResponse r = context.Response;
r.WriteFile("../Pictures/300/" + dt.Rows[0]["img"]);
HttpContext.Current.ApplicationInstance.CompleteRequest();
context.Response.Flush();
context.Response.Close();
context.Response.End();
}
}
图像在 Chrome 浏览器中看起来像这样;
您没有发送 content-Length
。它可能会弄乱 Chrome 中的图像(和其他文件)。当然,假设文件已正确保存在数据库中。
public void ProcessRequest(HttpContext context)
{
//create a new byte array
byte[] bin = new byte[0];
//get the item from a datatable
bin = (byte[])dt.Rows[0]["img"];
//read the image in an `Image` and then get the bytes in a memorystream
Image img = Image.FromFile(context.Server.MapPath("test.jpg"));
using (var ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
bin = ms.ToArray();
}
//or as one-liner
bin = File.ReadAllBytes(context.Server.MapPath("test.jpg"));
//clear the buffer stream
context.Response.ClearHeaders();
context.Response.Clear();
context.Response.Buffer = true;
//set the correct ContentType
context.Response.ContentType = "image/jpeg";
//set the filename for the image
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"myImage.jpg\"");
//set the correct length of the string being send
context.Response.AddHeader("content-Length", bin.Length.ToString());
//send the byte array to the browser
context.Response.OutputStream.Write(bin, 0, bin.Length);
//cleanup
context.Response.Flush();
context.ApplicationInstance.CompleteRequest();
}
imagehandler.ashx 图片在 Chrome 浏览器中不显示。我该如何修复它..?
我的代码 (imagehandler.ashx):
public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["YazarID"] != null)
{
string YazarID = context.Request.QueryString["YazarID"];
DataTable dt = new DataTable();
string query = "select img from Register where YazarID='" + YazarID + "'";
dt = Database.GetData(query);
HttpResponse r = context.Response;
r.WriteFile("../Pictures/300/" + dt.Rows[0]["img"]);
HttpContext.Current.ApplicationInstance.CompleteRequest();
context.Response.Flush();
context.Response.Close();
context.Response.End();
}
}
图像在 Chrome 浏览器中看起来像这样;
您没有发送 content-Length
。它可能会弄乱 Chrome 中的图像(和其他文件)。当然,假设文件已正确保存在数据库中。
public void ProcessRequest(HttpContext context)
{
//create a new byte array
byte[] bin = new byte[0];
//get the item from a datatable
bin = (byte[])dt.Rows[0]["img"];
//read the image in an `Image` and then get the bytes in a memorystream
Image img = Image.FromFile(context.Server.MapPath("test.jpg"));
using (var ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
bin = ms.ToArray();
}
//or as one-liner
bin = File.ReadAllBytes(context.Server.MapPath("test.jpg"));
//clear the buffer stream
context.Response.ClearHeaders();
context.Response.Clear();
context.Response.Buffer = true;
//set the correct ContentType
context.Response.ContentType = "image/jpeg";
//set the filename for the image
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"myImage.jpg\"");
//set the correct length of the string being send
context.Response.AddHeader("content-Length", bin.Length.ToString());
//send the byte array to the browser
context.Response.OutputStream.Write(bin, 0, bin.Length);
//cleanup
context.Response.Flush();
context.ApplicationInstance.CompleteRequest();
}