如下您可以使用一个HTTP处理程序:
public class GetImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
DateTime now = DateTime.Now;
context.Response.Cache.SetExpires(now.AddYears(1));
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetValidUntilExpires(true);
context.Response.Cache.SetLastModified(now);
context.Response.Cache.VaryByParams["FileID"] = true;
context.Response.Cache.SetOmitVaryStar(true);
context.Response.ContentType = file.ContentType;
context.Response.AppendHeader("content-length", file.ContentLength.ToString());
//TODO: Get your file here
string contentDisposition = String.Empty;
contentDisposition += "filename=" + file.OriginalFilename;
context.Response.AppendHeader("content-disposition", contentDisposition);
string imagePath = Path.Combine(HostingEnvironment.MapPath(Settings.Default.MediaPath), file.LocalFilename);
context.Response.WriteFile(imagePath);
}
public bool IsReusable
{
get
{
return false;
}
}
}
,并使用它像这样:
<img src="/Handlers/GetImage.ashx?FileID=' + thumbnailFileID + '" alt="" />