Get images from database in Entity Framework by passing id into ashx

余生颓废 提交于 2020-01-25 08:03:07

问题


In my homepage I Have a list of articles. When I click on any article it redirects to next page to view images of that article. in URL I am passing article id (pr_id) to next page. So to display resized images I am using handler. I need to display all the images which are associated to that pr_id.

How do I get all the images which are stored in DB by passing id in Entity Framework. I need to get all the images which are associate to that pr_id. I don't know where I am kind of stuck. In my DB pr_id = 1362 has 3 images and in view I see 3 placeholders for that pr_id but it is broken or not displaying image.

When I debug ImageHandler.ashx

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

fileName is showing null.

Do I need to get all the images by passing pr_id into the ashx file? If so how do I do that?

Image handler : 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;
        }
    } 
}

View:

@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

来源:https://stackoverflow.com/questions/50930432/get-images-from-database-in-entity-framework-by-passing-id-into-ashx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!