110,538
社区成员
发帖
与我相关
我的任务
分享
public void ProcessRequest(HttpContext context)
{
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
if (string.IsNullOrEmpty(Request.QueryString["ID"]))
return;
int ID = Convert.ToInt32(Request.QueryString["ID"]);
Attachment a = AttachmentClass.GetAttachment(ID);
if (a == null)
{//附件不存在
throw new Exception("Attachment_NotFound");
}
string path = Path.Combine(Sunivo.Components.Configuration.BaseSettings.UpLoadFilePath, a.FilePath);
if (!File.Exists(path))
{
throw new Exception("Attachment_NotFound");
//文件已被删除
}
//检查客户端缓存
if (CachedVersionIsOkay(a.UploadDate.ToUniversalTime(), Request))
{
Response.StatusCode = 304;
Response.SuppressContent = true;
return;
}
Response.ContentType = a.ContentType.ToLower();//MIME类型,和你那段代码相同
Response.Cache.SetLastModified(a.UploadDate);
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(a.Filename) + "\"");
Response.TransmitFile(path);
}
private bool CachedVersionIsOkay(DateTime lastWriteTime, HttpRequest Request)
{
string ifModified = Request.Headers["If-Modified-Since"];
if (ifModified != null)
{
string lastModified = lastWriteTime.ToString("r");
return (ifModified == lastModified);
}
return false;
}