【发布时间】:2012-03-22 16:10:12
【问题描述】:
我正在开发一个 Web 应用程序,因为我需要将图像文件和其他一些文档存储在数据库中,然后我必须最好将它们显示在图像框中的网页上。我已将图像存储在数据库中,还以字节数组的形式存储了 PDF 文件,我使用通用处理程序从数据库中获取字节数组,然后将它们显示在图像框中,它适用于图像,但不适用于 PDF 文件.
public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Int32 imgID;
if (context.Request.QueryString["id"] != null)
imgID = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/jpeg";
byte[] buffer = ShowEmpImage(imgID);
context.Response.BinaryWrite(buffer);
}
public Stream ShowEmpImage(int empno)
{
dbClass db = new dbClass();
string sql = "SELECT imgfile FROM myFiles WHERE id = @ID";
SqlCommand cmd = new SqlCommand(sql, db.con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", empno);
object img = null;
try
{
db.Connect();
img = cmd.ExecuteScalar();
}
catch
{
return null;
}
finally
{
db.Disconnect();
}
return new MemoryStream((byte[])img);
}
public bool IsReusable
{
get
{
return false;
}
}
}
请指导我如何在图像框中显示 PDF 文件和一些其他文件?实际上问题是我的应用程序与现有应用程序一起运行并且无法存储文件的扩展名所以我可以显示文件的唯一方法是“将它们显示为图像”但如果它们是更好的方法然后帮助我朋友.
【问题讨论】:
标签: c# asp.net sql-server-2008 asp.net-ajax