【发布时间】:2012-01-04 05:55:48
【问题描述】:
好的,我一直在为此不停地工作并进行大量搜索。从数据库中提取图像时,我无法显示它们。如果我尝试手动访问处理程序链接,我会收到一条消息“无法显示图像 [图像],因为它包含错误”。我以前在数据库中有一些旧图像,它首先正确显示了这些图像。但是现在如果我更新图像,它会在尝试查看它们时给我这个错误。
上传代码。
if (fileuploadImage.HasFile)
{
if (IsValidImage(fileuploadImage))
{
int length = fileuploadImage.PostedFile.ContentLength;
byte[] imgbyte = new byte[length];
HttpPostedFile img = fileuploadImage.PostedFile;
img.InputStream.Read(imgbyte, 0, length);
if (mainImage == null)
{
ProfileImage image = new ProfileImage();
image.ImageName = txtImageName.Text;
image.ImageData = imgbyte;
image.ImageType = img.ContentType;
image.MainImage = true;
image.PersonID = personID;
if (image.CreateImage() <= 0)
{
SetError("There was an error uploading this image.");
}
}
else
{
mainImage.ImageName = txtImageName.Text;
mainImage.ImageType = img.ContentType;
mainImage.ImageData = imgbyte;
mainImage.MainImage = true;
mainImage.PersonID = personID;
if (!mainImage.UpdateImage())
{
SetError("There was an error uploading this image.");
}
}
}
else
{
SetError("Not a valid image type.");
}
这是我的图像处理程序:
public class ImageHandler : IHttpHandler
{
public bool IsReusable
{
get
{
return false;
}
}
public void ProcessRequest(HttpContext context)
{
int imageid = Parser.GetInt(context.Request.QueryString["ImID"]);
ProfileImage image = new ProfileImage(Parser.GetInt(imageid));
context.Response.ContentType = image.ImageType;
context.Response.Clear();
context.Response.BinaryWrite(image.ImageData);
context.Response.End();
}
这就是我如何称呼它“~/ImageHandler.ashx?ImID=" + Parser.GetString(image.ImageID)
我在 sql server 中使用数据类型 Image 来存储这个。
编辑:
我还发现,如果我在 context.Response.end() 周围放置一个 try catch,它会出错说“无法评估代码,因为本机框架......”
【问题讨论】:
标签: asp.net visual-studio-2010 image c#-4.0 httphandler