最近有个需求是这样的,希望当用户在请求下载word文件时,进行拦截然后将word文件转为PDF后返回给用户,有哪位高手知道通过HttpModule该怎么做,希望能给个代码实例

解决方案 »

  1.   

    public void ProcessRequest(HttpContext context)
      {
         判断context.Request.FilePath,然后处理
      }
      

  2.   

    2楼的这位高手可能给出详细点的代码,因为对HttpModule并不熟悉
      

  3.   


    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>
      

  4.   

    试了不行,当点击要下载的word文件时,还是照样能看到原word文件而不是那个转换后的txt文件
      

  5.   

    试了没问题
    下载下来的也是txt那个文本
      

  6.   

    瞎折腾对于 .txt或.doc类的HTTP请求
    还轮不着asp.net说话,级别不够。
    这是由IIS来处理的除非你在IIS中配置,将 .txt或.doc交给asp.net处理。
    然后你的HttpModule中的相关逻辑才能生效。
      

  7.   

    我试了如果直接访问文件的URL地址是可能,但是如果在页面中点击word文件的链接地址,就直接在Office word里打开原文件内容了
      

  8.   

    你那是只是改了扩展名,并没有真的转成pdf,你需要使用真正转成pdf
    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/f8989c05-d04a-4b4a-be0f-fc0055691de7/
      

  9.   

    你要是只变扩展名,楼上的方法就可以了。
    地址栏输入和链接点击道理是一样的,都是http请求。
      

  10.   

    晕忽忽,哈呵呵,“当用户在请求下载word文件时”真的是神经,你的系统提供这些么?如果提供,为什么不走apsx或者ashx渠道,而是走doc“后门”。
      

  11.   

    很简单,所有aspx的开发人员都知道,通过提供类似<a href="http:/xxxx/downloand.aspx?name=xxx.doc“>xxx.doc</a> 这样的html element来显示连接啊!
      

  12.   

    处理特定类型的文件 还是 实现HttpHandler好点吧
      

  13.   

    你先读取  在加载在PDF 或者读取了生成 PDF  不是 word转PDF
      

  14.   

    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
        }