我有一个系统,您可以在其中查看照片作为记录的额外细节。让我们说这是一张员工猫的照片 这些照片存储在我们的数据库中。
目前有人希望查看小猫。我们会像这样呈现img
标记。
<a href="#" onclick="RenderTag('20');return false;">View Kitty</a>
<div id="imageDiv"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function RenderTag(id){
$('#imageDiv').html('<img src="http://localhost/GetKitty.aspx?ID=' + id + '" />');
}
</script>
GetKitty.aspx就像这样工作
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("Content-Disposition","attachment;FileName=kitty.bmp");
Response.ContentType = "image/bmp";
Response.Cache.SetLastModified(DateTime.Now);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Bitmap kitty = GetKittyBitmap(Request.QueryString("ID"));
kitty.Save(Response.OutputStream, kitty.RawFormat);
Response.Flush();
Response.Close();
kitty.Dispose();
}
单击锚链接后,页面就像这样
问题在于,有时小猫没有出现在我们的移动设备上,我的用户会感到沮丧,并打电话给办公室里的人用他们的手机拍摄小猫的照片,然后通过电子邮件将小猫照片发给他们。
对小猫的电子邮件图片的需求非常大,他们现在要求发送小猫图片的电子邮件功能内置到系统中。
如果小猫的完整图片只是按预期呈现,那么可以避免构建该功能。
是否有更可靠的方法来显示存储在数据库中的图像?
答案 0 :(得分:0)
您可以尝试使用jQuery .load函数来提供错误处理功能(并可能重新启动获取图像),如this SO post所示。
答案 1 :(得分:0)
虽然我认为这种方法没有问题,但您应该将其实现为HttpHandler(.ashx)
如果不需要在整个Page生命周期后面运行aspx页面,Kitty就会更快地离开你的服务器。
由于您是通过网络发送此图片,请考虑使用压缩格式,如jpg或png。位图很大。你能更快地发送kitty。
加载测试您的应用程序,从多个客户端下载几次并保存文件。 IIS有时会遇到可以处理的同时请求数量的限制。
如果您在页面上有一个包含多个这样的小图像的页面,那就是对asp.net的许多请求。 asp.net 2.0上的默认线程数仅为25。
答案 2 :(得分:0)
好的,我发现了一种可靠的方法让猫咪总是呈现!
如果我添加Content-Length
标题,则可以解决问题。
我这样实现它。
using (MemoryStream inputMS = NewMemoryStream(kittyBytes))
using (Bitmap kittyBitmap = new Bitmap(inputMS))
{
Response.Clear();
Response.Cache.SetLastModified(DateTime.Now);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Some logic goes here determining the image type of the kitty
//We send low rez jpgs at certain times
string kittyFileSuffix = ".bmp";
string kittyContentType = "image/bmp";
Response.AddHeader("Content-Disposition", "attachment;FileName=Kitty" + kittyFileType);
Response.ContentType = kittyContentType;
using (Image smallKitty = ResizeKitty(kittyBitmap))
{
long quality = 40;
Imaging.EncoderParameters ep = new Imaging.EncoderParameters(1);
ep.Param[0] = new Imaging.EncoderParameter(Imaging.Encoder.Quality, quality);
using (MemoryStream outputMS = new MemoryStream())
{
smallKitty.Save(outputMS, GetImageCodecInfo(kittyContentType), ep);
Response.AddHeader("Content-Length", outputMS.Length);
outputMS.WriteTo(Response.OutputStream);
Response.Flush();
Response.Close():
}
}
}