Muh*_*eel 5 c# asp.net asp.net-ajax sql-server-2008
我正在开发一个Web应用程序,我需要将图像文件和一些其他文档存储在数据库中,然后我必须在图像框中的网页上显示它们.我已经在数据库中存储了图像,也以字节数组的形式存储了PDF文件,我使用Generic Handler从数据库中获取字节数组,然后在图像框中显示它对图像工作正常,但它不适用于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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
请指导我如何在图像框中显示PDF文件和其他一些文件?实际上问题是我的应用程序运行与现有的应用程序,并没有办法存储文件的扩展,所以我可以显示文件的唯一方法是"将它们显示为图像",但如果他们是一个更好的方式然后帮助我的朋友.