Moran
Moran

Reputation: 435

How to access the handler even if the file does not exist?

Im using - asp.net mvc.

targetFramework="4.6.1"

I created a handler that need to check if the image exist in the local folder , if exist - return the image , if not - it go to the server url image path and download from there to the local folder. and than return the image.

MyHandler : HttpImageHandler.cs

using SearchContentPortal.Core;
using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Web;

namespace SearchContentPortal.Handlers
{
    public class HttpImageHandler : IHttpHandler
    {

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Clear();
            context.Response.ClearContent();
            context.Response.ClearHeaders();
            var fileName = Path.GetFileName(context.Request.PhysicalPath);
            var localfilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Images\media", fileName);
            //check if file not exists in local folder,  or the last modify file wasn't today
            if (!File.Exists(localfilePath) || (File.Exists(localfilePath) && (File.GetLastWriteTime(localfilePath).ToShortDateString() != DateTime.Today.ToString("dd/MM/yyyy"))))
            { //if true - download the file to local folder
                DownloadAndSaveImage(localfilePath, fileName);
            }

            //return file stream
            Bitmap bitmap = new Bitmap(context.Server.MapPath("~/Images/media/" + fileName));

            using (MemoryStream ms = new MemoryStream())
            {
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                ms.WriteTo(context.Response.OutputStream);
                context.Response.ContentType = "image/png";
            }

            context.Response.Flush();
            context.Response.End();
        }

        public static void DownloadAndSaveImage(string localfilePath, string fileName)
        {
            using (WebClient client = new WebClient())
            {
                client.DownloadFile(AppSettings.ImagesBaseUrl + "/" + fileName, localfilePath);
            }
        }

    }
}

My Web.config

  <system.webServer>
    <handlers>
      <add name="HttpImageHandler1" path="*.png" verb="*" 
      type="SearchContentPortal.Handlers.HttpImageHandler" 
      preCondition="integratedMode,runtimeVersionv4.0" />
      <add name="HttpImageHandler2" path="*.jpg" verb="*" 
      type="SearchContentPortal.Handlers.HttpImageHandler" 
      preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>

My image -

 <img src="~/Images/media/beauty_beast.jpg" alt="beauty and the beast"  />

But it not works` - not entered the handler.

And if i have the image physically in the "media" folder already. (if exist) - it entered to the hadler.

And i can't use in src - "Handlers/ImageHandler.ashx?fileName=beauty_beast.jpg". I mean without using ashx file ( HttpImageHandler.ashx ) like :

 <img src="Handlers/ImageHandler.ashx?fileName=beauty_beast.jpg" alt="beauty and the beast"/>

i want to see in the src the path like this:

 <img src="~/Images/media/beauty_beast.jpg" alt="beauty and the beast"/>

What is the right solution?

thanks!!

Upvotes: 2

Views: 1234

Answers (1)

Daniel B
Daniel B

Reputation: 3185

In ASP.Net Web Forms, it will work fine but in MVC, you need to ignore this path to be not treated as controller-action path so:

routes.IgnoreRoute("Images/{*pathInfo}");

or generally

routes.IgnoreRoute("Images/");

ASP.NET Routing - Ignore routes for files with specific extension, regardless of directory

Upvotes: 1

Related Questions