<%@ WebHandler Language="C#" Class="PDF" %>using System;
using System.Web;
using System.Diagnostics;
using System.IO;public class PDF : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        string url = context.Request["url"];
        string path = HttpContext.Current.Server.MapPath("~/PDF/");        HtmlToPdf(url,path);
               //下载信息,这里下载不了文件啊。高手指教
        FileInfo info = new FileInfo(filePath);
        long fileSize = info.Length;
        context.Response.Clear();
        context.Response.ContentType = "application/x-zip-compressed";
        context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
        //不指明Content-Length用Flush的话不会显示下载进度 
        context.Response.AddHeader("Content-Length", fileSize.ToString());
        context.Response.TransmitFile(filePath, 0, fileSize);
        context.Response.Flush();
        context.Response.Close();
        
       
    }    public static bool HtmlToPdf(string url, string path)
    {
        try
        {
           
            if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path))
                return false;
            Process p = new Process();
            string str = System.Web.HttpContext.Current.Server.MapPath("~/wkhtmltopdf.exe");
            string filename = path + "Artistic.pdf";
            if (!System.IO.File.Exists(str))
                return false;
            p.StartInfo.FileName = str;
            p.StartInfo.Arguments = " \"" + url + "\" " + path;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
           
            p.StartInfo.Arguments = string.Format("  \"{0}\" \"{1}\" --password \"{2}\" --username \"{3}\"", url, filename, "123", "admin","");
            p.Start();
            System.Threading.Thread.Sleep(100);            return true;
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex);
        }
        return true;
    }
    
 
    public bool IsReusable {
        get {
            return false;
        }
    }}URLStringPathPDF

解决方案 »

  1.   

    using System;    
    using System.Web;    
    public class download : IHttpHandler {    
           
        public void ProcessRequest (HttpContext context) {    
            string url = HttpContext.Current.Server.UrlDecode(context.Request["url"]);    
            downloadfile(url);    
        }    
         
        public bool IsReusable {    
            get {    
                return false;    
            }    
        }    
        public void downloadfile(string s_fileName)    
        {    
           HttpContext.Current.Response.ContentType = "application/ms-download";    
           string s_path = HttpContext.Current.Server.MapPath("~/") + s_fileName;    
           System.IO.FileInfo file = new System.IO.FileInfo(s_path);    
           HttpContext.Current.Response.Clear();    
           HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");    
           HttpContext.Current.Response.Charset = "utf-8";    
           HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));    
           HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());    
           HttpContext.Current.Response.WriteFile(file.FullName);    
           HttpContext.Current.Response.Flush();    
           HttpContext.Current.Response.Clear();    
           HttpContext.Current.Response.End();    
        }    
    }   $.post("action/download.ashx",{url:result});为什么实现不了下载啊。
      

  2.   

    没有异常啊。 执行到这一句HttpContext.Current.Response.End();  然后就没反应了。  
      

  3.   

    Ajax下载文件我倒没弄成过
    把    $.post("pdf.ashx", { url: 'Silverlight.js' });改成:    window.location = "pdf.ashx?url=Silverlight.js";
      

  4.   

    js能下载文件的代码真没见过,见过用js和iframe下载文件的就见过。