如何通过HttpModule拦截用户下载文件 最近有个需求是这样的,希望当用户在请求下载word文件时,进行拦截然后将word文件转为PDF后返回给用户,有哪位高手知道通过HttpModule该怎么做,希望能给个代码实例 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public void ProcessRequest(HttpContext context) { 判断context.Request.FilePath,然后处理 } 2楼的这位高手可能给出详细点的代码,因为对HttpModule并不熟悉 namespace ClassLibrary1{ public class Class1:IHttpModule { public void Dispose() { } public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); } void context_BeginRequest(object sender, EventArgs e) { HttpContext context = HttpContext.Current; string filename = Path.GetFileName(context.Request.PhysicalPath); string exten = Path.GetExtension(filename); if (exten == ".doc") {//这里如果请求的是doc后缀名的话我把它转成txt格式文件下载,你把下面部分换成你转成pdf的代码,//然后下载下来。 FileInfo f = new FileInfo(context.Server.MapPath("TextFile.txt")); context.Response.AddHeader("Content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(f.Name)); context.Response.ContentType = "application/octet-stream"; context.Response.AddHeader("content-length", f.Length.ToString()); context.Response.WriteFile(f.FullName); context.Response.End(); } } }}web.config <httpModules> <add name="WordModule" type="ClassLibrary1.Class1,ClassLibrary1"/> </httpModules> 试了不行,当点击要下载的word文件时,还是照样能看到原word文件而不是那个转换后的txt文件 试了没问题下载下来的也是txt那个文本 瞎折腾对于 .txt或.doc类的HTTP请求还轮不着asp.net说话,级别不够。这是由IIS来处理的除非你在IIS中配置,将 .txt或.doc交给asp.net处理。然后你的HttpModule中的相关逻辑才能生效。 我试了如果直接访问文件的URL地址是可能,但是如果在页面中点击word文件的链接地址,就直接在Office word里打开原文件内容了 你那是只是改了扩展名,并没有真的转成pdf,你需要使用真正转成pdfhttp://social.msdn.microsoft.com/Forums/en-US/vsto/thread/f8989c05-d04a-4b4a-be0f-fc0055691de7/ 你要是只变扩展名,楼上的方法就可以了。地址栏输入和链接点击道理是一样的,都是http请求。 晕忽忽,哈呵呵,“当用户在请求下载word文件时”真的是神经,你的系统提供这些么?如果提供,为什么不走apsx或者ashx渠道,而是走doc“后门”。 很简单,所有aspx的开发人员都知道,通过提供类似<a href="http:/xxxx/downloand.aspx?name=xxx.doc“>xxx.doc</a> 这样的html element来显示连接啊! 处理特定类型的文件 还是 实现HttpHandler好点吧 你先读取 在加载在PDF 或者读取了生成 PDF 不是 word转PDF public class SafeDocHttpHandler:IHttpHandler { private HttpRequest Request { get { return HttpContext.Current.Request; } } private HttpResponse Response { get { return HttpContext.Current.Response; } } #region IHttpHandler 成员 public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { SPSite site = SPControl.GetContextSite(context); string userName = context.User.Identity.Name; if (site.RootWeb.CurrentUser != null || (userName != null && userName != string.Empty)) { SPUser curuser = site.RootWeb.CurrentUser; if (userName == string.Empty) userName = curuser.LoginName; if (userName == null || userName == string.Empty || !context.User.Identity.IsAuthenticated) { context.Server.Transfer("/_layouts/login.aspx"); return; } else { string downUserName = SPContext.Current.Web.CurrentUser.Name; string filename = Path.GetFileName(context.Request.PhysicalPath); string exten = Path.GetExtension(filename); //if (exten.Contains(".doc")&&!context.Request.Url.ToString().Contains("/chengguo")) if (exten.Contains(".doc") || exten.Contains(".xls") || exten.Contains(".ppt")) { ws_doc27.WebService_Doc wsdoc = new ws_doc27.WebService_Doc(); wsdoc.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; AttachEntity pdfAttach = new AttachEntity(); //调用DOC转PDF处理WEBSERVICE bool result = wsdoc.DocToPDF(context.Request.FilePath, downUserName,out pdfAttach); if (result) { context.Response.Clear(); context.Response.ClearHeaders(); context.Response.ClearContent(); context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9"); context.Response.ContentType = "application/" + "pdf"; context.Response.Charset = "windows-1254"; context.Response.AddHeader("Content-Length", pdfAttach.Data.Length.ToString()); context.Response.AddHeader("content-disposition", "attachment; filename=\"" + HttpUtility.UrlPathEncode(System.IO.Path.GetFileName(pdfAttach.FileName)) + "\""); context.Response.BinaryWrite(pdfAttach.Data); context.Response.Flush(); context.Response.End(); } else { context.Response.Write("文件无效无法下载!"); context.Response.End(); } } } } } #endregion } 求js代码,能在右下角弹出小窗口,带过渡效果! 一个小问题 求助:asp.net 动态生成表单控件RadioButton,大约在数量多于200个时提交就会出错 关于static和继承的问题 一个编辑器的问题 sql2000 上传数据库问题 如何把一个控价动态加载到指定的坐标上比如(720,400,836,424) 各位高手 我有个问题请教 请大家进来看看谢谢 通过ADO.Net修改access数据库的问题 如何理解Web表单不支持OutputCache的varybycontrol属性? vs2008下CrystalReportViewer错误 未预编译文件
{
判断context.Request.FilePath,然后处理
}
namespace ClassLibrary1
{
public class Class1:IHttpModule
{
public void Dispose()
{
} public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
} void context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
string filename = Path.GetFileName(context.Request.PhysicalPath);
string exten = Path.GetExtension(filename);
if (exten == ".doc")
{
//这里如果请求的是doc后缀名的话我把它转成txt格式文件下载,你把下面部分换成你转成pdf的代码,
//然后下载下来。
FileInfo f = new FileInfo(context.Server.MapPath("TextFile.txt"));
context.Response.AddHeader("Content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(f.Name));
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("content-length", f.Length.ToString());
context.Response.WriteFile(f.FullName);
context.Response.End();
}
}
}
}web.config
<httpModules>
<add name="WordModule" type="ClassLibrary1.Class1,ClassLibrary1"/>
</httpModules>
下载下来的也是txt那个文本
还轮不着asp.net说话,级别不够。
这是由IIS来处理的除非你在IIS中配置,将 .txt或.doc交给asp.net处理。
然后你的HttpModule中的相关逻辑才能生效。
http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/f8989c05-d04a-4b4a-be0f-fc0055691de7/
地址栏输入和链接点击道理是一样的,都是http请求。
{
private HttpRequest Request
{
get { return HttpContext.Current.Request; }
}
private HttpResponse Response
{
get { return HttpContext.Current.Response; }
}
#region IHttpHandler 成员
public bool IsReusable
{
get { return false; }
} public void ProcessRequest(HttpContext context)
{
SPSite site = SPControl.GetContextSite(context);
string userName = context.User.Identity.Name;
if (site.RootWeb.CurrentUser != null || (userName != null && userName != string.Empty))
{
SPUser curuser = site.RootWeb.CurrentUser; if (userName == string.Empty)
userName = curuser.LoginName; if (userName == null || userName == string.Empty || !context.User.Identity.IsAuthenticated)
{
context.Server.Transfer("/_layouts/login.aspx");
return;
}
else
{
string downUserName = SPContext.Current.Web.CurrentUser.Name;
string filename = Path.GetFileName(context.Request.PhysicalPath);
string exten = Path.GetExtension(filename); //if (exten.Contains(".doc")&&!context.Request.Url.ToString().Contains("/chengguo"))
if (exten.Contains(".doc") || exten.Contains(".xls") || exten.Contains(".ppt"))
{
ws_doc27.WebService_Doc wsdoc = new ws_doc27.WebService_Doc();
wsdoc.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
AttachEntity pdfAttach = new AttachEntity();
//调用DOC转PDF处理WEBSERVICE
bool result = wsdoc.DocToPDF(context.Request.FilePath, downUserName,out pdfAttach);
if (result)
{
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
context.Response.ContentType = "application/" + "pdf";
context.Response.Charset = "windows-1254";
context.Response.AddHeader("Content-Length", pdfAttach.Data.Length.ToString());
context.Response.AddHeader("content-disposition", "attachment; filename=\"" + HttpUtility.UrlPathEncode(System.IO.Path.GetFileName(pdfAttach.FileName)) + "\"");
context.Response.BinaryWrite(pdfAttach.Data);
context.Response.Flush();
context.Response.End();
}
else
{
context.Response.Write("文件无效无法下载!");
context.Response.End();
}
}
}
} } #endregion
}