HttpHandler [图像] 无法显示,因为它包含错误

发布于 2024-12-17 18:18:31 字数 2511 浏览 0 评论 0原文

好吧,我一直在不停地研究这个问题并进行了大量的搜索。从数据库中提取图像时,我无法显示图像。如果我尝试手动转到处理程序链接,我会收到一条消息“无法显示图像 [Image],因为它包含错误”。我之前在数据库中有一些旧图像,它首先正确地显示了这些图像。但现在如果我更新图像,在尝试查看它们时会出现此错误。

上传代码。

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 ,它会错误地显示“无法评估代码,因为本机框架......”

Ok i have been working non stop on this and doing a lot of searching. I cannot get my images to display when pulling them from the database. If i try going to the handler link manually i get a message saying "The image [Image] cannot be displayed because it contains errors". I had some old images in the database from before and it first displayed those correctly. But now if i update images it will give me this error when trying to view them.

Upload code.

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.");
            }

Here is my image handler:

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();
    }

And this is how i'm calling it "~/ImageHandler.ashx?ImID=" + Parser.GetString(image.ImageID)

I'm using the data type Image in sql server to store this.

Edit:

I also found out that if i put a try catch around context.Response.end() it is erroring out saying the "Unable to evaluate the code because the native frame..."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌逼全场 2024-12-24 18:18:31

我发现了我的问题。我正在检查实际文件的标头以确保它有效。不知何故,这改变了数据并使其变得糟糕。

I found my problem. I was checking the header of the actual file to make sure it was valid. Somehow that was altering the data and making it bad.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文