即使文件不存在,如何访问处理程序?
How to access the handler even if the file does not exist?
我正在使用 - asp.net mvc.
targetFramework="4.6.1"
我创建了一个处理程序,需要检查图像是否存在于本地文件夹中,如果存在 - return 图像,如果不存在 - 它转到服务器 url 图像路径并下载从那里到本地文件夹。而不是 return 图像。
我的处理程序: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);
}
}
}
}
我的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>
我的形象 -
<img src="~/Images/media/beauty_beast.jpg" alt="beauty and the beast" />
但它不起作用` - 未进入处理程序。
并且 如果我已经在 "media" 文件夹中物理 了图像。 (如果存在)- 它进入了 hadler。
而且我无法在 src 中使用 - "Handlers/ImageHandler.ashx?fileName=beauty_beast.jpg"。
我的意思是不使用 ashx 文件(HttpImageHandler.ashx),例如:
<img src="Handlers/ImageHandler.ashx?fileName=beauty_beast.jpg" alt="beauty and the beast"/>
我想在src中看到路径
像这样:
<img src="~/Images/media/beauty_beast.jpg" alt="beauty and the beast"/>
什么是正确的解决方案?
谢谢!!
在 ASP.Net Web Forms 中,它可以正常工作,但在 MVC 中,您需要忽略此路径,以免被视为控制器操作路径,因此:
routes.IgnoreRoute("Images/{*pathInfo}");
或通常
routes.IgnoreRoute("Images/");
ASP.NET Routing - Ignore routes for files with specific extension, regardless of directory
我正在使用 - asp.net mvc.
targetFramework="4.6.1"
我创建了一个处理程序,需要检查图像是否存在于本地文件夹中,如果存在 - return 图像,如果不存在 - 它转到服务器 url 图像路径并下载从那里到本地文件夹。而不是 return 图像。
我的处理程序: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);
}
}
}
}
我的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>
我的形象 -
<img src="~/Images/media/beauty_beast.jpg" alt="beauty and the beast" />
但它不起作用` - 未进入处理程序。
并且 如果我已经在 "media" 文件夹中物理 了图像。 (如果存在)- 它进入了 hadler。
而且我无法在 src 中使用 - "Handlers/ImageHandler.ashx?fileName=beauty_beast.jpg"。 我的意思是不使用 ashx 文件(HttpImageHandler.ashx),例如:
<img src="Handlers/ImageHandler.ashx?fileName=beauty_beast.jpg" alt="beauty and the beast"/>
我想在src中看到路径 像这样:
<img src="~/Images/media/beauty_beast.jpg" alt="beauty and the beast"/>
什么是正确的解决方案?
谢谢!!
在 ASP.Net Web Forms 中,它可以正常工作,但在 MVC 中,您需要忽略此路径,以免被视为控制器操作路径,因此:
routes.IgnoreRoute("Images/{*pathInfo}");
或通常
routes.IgnoreRoute("Images/");
ASP.NET Routing - Ignore routes for files with specific extension, regardless of directory