通过将ID传递给ashx从Entity Framework中的数据库获取图像

时间:2018-06-19 14:04:45

标签: c# asp.net-mvc entity-framework httphandler

在我的主页上,有文章列表。当我单击任何文章时,它将重定向到下一页以查看该文章的图像。在URL中,我正在将文章ID(pr_id)传递到下一页。因此,要显示调整大小的图像,我正在使用处理程序。我需要显示与该pr_id相关的所有图像。

如何通过在Entity Framework中传递id来获取存储在DB中的所有图像。我需要获取与该pr_id相关的所有图像。我不知道我在哪里卡住了。在我的数据库pr_id = 1362中有3张图片,并且在视图中我看到该pr_id的3个占位符,但是它已损坏或没有显示图片。

当我调试ImageHandler.ashx

string fileName = context.Request.QueryString["file"]; 

fileName显示为空。

我是否需要通过将pr_id传递到ashx文件中来获取所有图像?如果是这样,我该怎么办?

图像处理程序:ImageHandler.ASHX

namespace PressRoom
{   
    public class ImageHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string fileName = context.Request.QueryString["file"];
            string filePath = context.Server.MapPath("~/Images/Releases/" + fileName);

            context.Response.AddHeader("content-disposition", 
            string.Format("attachment; filename={0}", fileName));

            if (File.Exists(filePath))
            {
                byte[] bytes = File.ReadAllBytes(filePath);
                context.Response.BinaryWrite(bytes);
            }
            else
            {
                 throw new HttpException(404, "Invalid photo name.");
            }
        }

        public bool IsReusable
        {
            get { return true; }
        }

        byte[] getResizedImage(String path, int width, int height)
        {
            Bitmap imgIn = new Bitmap(path);

            double y = imgIn.Height;
            double x = imgIn.Width;

            double factor = 1;

            if (width > 0)
            {
                factor = width / x;
            }
            else if (height > 0)
            {
                factor = height / y;
            }

            System.IO.MemoryStream outStream = new System.IO.MemoryStream();
            Bitmap imgOut = new Bitmap((int)(x * factor), (int)(y * factor));

            // Set DPI of image (xDpi, yDpi)
            imgOut.SetResolution(72, 72);

            Graphics g = Graphics.FromImage(imgOut);
            g.Clear(Color.White);
            g.DrawImage(imgIn, new Rectangle(0, 0, (int)(factor * x), (int)(factor * y)),
                     new Rectangle(0, 0, (int)x, (int)y), GraphicsUnit.Pixel);

            imgOut.Save(outStream, getImageFormat(path));

            return outStream.ToArray();
        }

        string getContentType(String path)
        {
            switch (Path.GetExtension(path))
            {
                case ".bmp": return "Image/bmp";
                case ".gif": return "Image/gif";
                case ".jpg": return "Image/jpeg";
                case ".png": return "Image/png";
                default: break;
            }

            return "";
        }

        ImageFormat getImageFormat(String path)
        {
            switch (Path.GetExtension(path))
            {
                case ".bmp": return ImageFormat.Bmp;
                case ".gif": return ImageFormat.Gif;
                case ".jpg": return ImageFormat.Jpeg;
                case ".png": return ImageFormat.Png;
                default: break;
            }

            return ImageFormat.Jpeg;
        }
    } 
}

查看:

@foreach (var photos in Model.Photos)
{
    <div class="slide_one slide">
        <img src="ImageHandler.ashx?file=@photos.photo_url" style="width:100px; height:100px;" />
    </div>
}

My database table looks like this

0 个答案:

没有答案