C# 当SQL BLOB不可用时,如何从.ashx加载默认映像
我是ASP&C的新手,一直不知道如何做到这一点 我通过C# 当SQL BLOB不可用时,如何从.ashx加载默认映像,c#,asp.net,blob,C#,Asp.net,Blob,我是ASP&C的新手,一直不知道如何做到这一点 我通过.ashx文件从bd加载BLOB,就像so一样,它工作正常,但有时没有BLOB或它是空的 这是基本代码 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DataSource.iMIS.Connection"].ConnectionString); SqlCommand cmd = new SqlComma
.ashx
文件从bd加载BLOB,就像so
一样,它工作正常,但有时没有BLOB或它是空的
这是基本代码
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DataSource.iMIS.Connection"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT PICTURE_LOGO FROM Name_Picture WHERE ID = @EmpID", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@EmpID", id);
con.Open();
byte[] pict = (byte[])cmd.ExecuteScalar();
con.Close();
ctx.Response.ContentType = "image/bmp";
ctx.Response.OutputStream.Write(pict, 0, pict.Length);
我的想法是在con.Close()
之后立即检查pict.Length
,如果失败,我想显示默认图像,甚至文本
可能吗?怎么做?经过更多的搜索和大量的“HttpCompileException(s)”,我终于找到了工作 感谢@Kazar在这里给出这个答案,感谢@Pranay Rana在这里给出这个答案 我把这些放在一起
con.Open();
byte[] pict = (byte[])cmd.ExecuteScalar();
con.Close();
ctx.Response.ContentType = "image/bmp";
if (pict.Length <= 1) {
// the BLOB is not a picture
byte[] txt = ImageToByteArray(DrawText("no image found"));
ctx.Response.OutputStream.Write(txt, 0, txt.Length);
} else {
// stream the picture data from BLOB
ctx.Response.OutputStream.Write(pict, 0, pict.Length);
}
con.Open();
字节[]pict=(字节[])cmd.ExecuteScalar();
con.Close();
ctx.Response.ContentType=“image/bmp”;
如果(pict.Length经过更多的搜索和大量的“HttpCompileException(s)”,我就可以工作了
感谢@Kazar在这里给出这个答案,感谢@Pranay Rana在这里给出这个答案
我把这些放在一起
con.Open();
byte[] pict = (byte[])cmd.ExecuteScalar();
con.Close();
ctx.Response.ContentType = "image/bmp";
if (pict.Length <= 1) {
// the BLOB is not a picture
byte[] txt = ImageToByteArray(DrawText("no image found"));
ctx.Response.OutputStream.Write(txt, 0, txt.Length);
} else {
// stream the picture data from BLOB
ctx.Response.OutputStream.Write(pict, 0, pict.Length);
}
con.Open();
字节[]pict=(字节[])cmd.ExecuteScalar();
con.Close();
ctx.Response.ContentType=“image/bmp”;
如果(pict.Length如果要在数据库中找不到映像时从磁盘加载映像,请使用此代码段
public void ProcessRequest(HttpContext context)
{
//create a new byte array
byte[] pict = new byte[0];
//create a connection to the db and a command
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DataSource.iMIS.Connection"].ConnectionString))
using (SqlCommand command = new SqlCommand("SELECT PICTURE_LOGO FROM Name_Picture WHERE ID = @EmpID", connection))
{
//set the proper command type
command.CommandType = CommandType.Text;
//replace the parameters
command.Parameters.Add("@EmpID", SqlDbType.Int).Value = id;
try
{
//open the db and execute the sql string
connection.Open();
pict = (byte[])command.ExecuteScalar();
}
catch (Exception ex)
{
//catch any errors like unable to open db or errors in command. view with ex.Message
}
}
//if no image found in database load the default from disk
if (pict == null || pict.Length == 0)
{
pict = File.ReadAllBytes(context.Server.MapPath("/noimage.bmp"));
}
//clear the buffer stream
context.Response.ClearHeaders();
context.Response.Clear();
context.Response.Buffer = true;
//set the correct ContentType
context.Response.ContentType = "image/bmp";
//set the filename for the image
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"ImageName.bmp\"");
//set the correct length of the string being send
context.Response.AddHeader("content-Length", pict.Length.ToString());
//send the byte array to the browser
context.Response.OutputStream.Write(pict, 0, pict.Length);
//cleanup
context.Response.Flush();
context.ApplicationInstance.CompleteRequest();
}
如果要在数据库中找不到映像时从磁盘加载映像,请使用此代码段
public void ProcessRequest(HttpContext context)
{
//create a new byte array
byte[] pict = new byte[0];
//create a connection to the db and a command
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DataSource.iMIS.Connection"].ConnectionString))
using (SqlCommand command = new SqlCommand("SELECT PICTURE_LOGO FROM Name_Picture WHERE ID = @EmpID", connection))
{
//set the proper command type
command.CommandType = CommandType.Text;
//replace the parameters
command.Parameters.Add("@EmpID", SqlDbType.Int).Value = id;
try
{
//open the db and execute the sql string
connection.Open();
pict = (byte[])command.ExecuteScalar();
}
catch (Exception ex)
{
//catch any errors like unable to open db or errors in command. view with ex.Message
}
}
//if no image found in database load the default from disk
if (pict == null || pict.Length == 0)
{
pict = File.ReadAllBytes(context.Server.MapPath("/noimage.bmp"));
}
//clear the buffer stream
context.Response.ClearHeaders();
context.Response.Clear();
context.Response.Buffer = true;
//set the correct ContentType
context.Response.ContentType = "image/bmp";
//set the filename for the image
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"ImageName.bmp\"");
//set the correct length of the string being send
context.Response.AddHeader("content-Length", pict.Length.ToString());
//send the byte array to the browser
context.Response.OutputStream.Write(pict, 0, pict.Length);
//cleanup
context.Response.Flush();
context.ApplicationInstance.CompleteRequest();
}
我是通过这篇文章来的,但我不明白我正在尝试实现这一点,但不知道如何“流”它我是通过这篇文章来的,但我不明白我正在尝试实现这一点,但不知道如何“流”它谢谢!这行代码pict=File.ReadAllBytes(context.Server.MapPath(“/noimage.bmp”));
正是我需要的。谢谢!这里的这一行pict=File.ReadAllBytes(context.Server.MapPath(“/noimage.bmp”))
正是我所需要的。这种方法的好处是,您可以输出错误消息,或者动态更改文本/pic。这种方法的好处是,您可以动态输出错误消息,或者更改文本/pic。