$.post("action/PDF.ashx",{url:url},function(result){
                // alert(result);
                 window.location.href = "PDF/" + result;
                 
                 hideBlock();
            });   window.location.href = "PDF/" + result; 执行到这里他就直接在网页中打开pdf,我想让他执行到这里然后显示下载或另存为,能实现吗,请高手指教
jQueryjsasp.net。net

解决方案 »

  1.   

    PDF.ashx文件需要以流输出才可以。
    Response.Clear();
    Response.Buffer = true;
    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Server.MapPath("~/PDF.asax"),System.Text.Encoding.UTF8));
    Response.WriteFile(Server.MapPath("~/PDF.asax"));
    Response.Flush();
    Response.Close();
      

  2.   


    jquery 里面怎么写啊。高手、、
      

  3.   

        public void ProcessRequest (HttpContext context) {
            context.Response.ContentType = "text/plain";
           // context.Response.Write("Hello World");
            string url = context.Request["url"];
            string be = url.Substring(url.LastIndexOf("/") + 1);
            string str = be.Substring(0, be.IndexOf("."));
            if (url != null)
            {
                string path1 = System.Web.HttpContext.Current.Server.MapPath("~/PDF/" + str + ".pdf");
                HtmlToPdf(url, path1);
            }
           // GC.Collect();
            context.Response.Write(str + ".pdf");
           // downloadfile(str + ".pdf");
        }ashx里面要返回数据到jquery里面判断然后才去执行下载
      

  4.   

    你post到ProcessRequest 方法,判断,不满足条件返回信息到页面,如果满足就直接
    Response.Clear();
    Response.Buffer = true;
    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Server.MapPath("~/PDF.asax"),System.Text.Encoding.UTF8));
    Response.WriteFile(Server.MapPath("~/PDF.asax"));
    Response.Flush();
    Response.Close();
      

  5.   

    <%@ WebHandler Language="C#" Class="Pdf" %>using System;
    using System.Web;
    using System.Diagnostics;public class Pdf : IHttpHandler {
        
        public void ProcessRequest (HttpContext context) {
            context.Response.ContentType = "text/plain";
            string url = context.Request["url"].ToString();
            string be = url.Substring(url.LastIndexOf("/") + 1);
            string str = be.Substring(0, be.IndexOf("."));
            if (url != null)
            {
                string path1 = System.Web.HttpContext.Current.Server.MapPath("~/PDF/" + str + ".pdf");
                HtmlToPdf(url, path1);
            }
            downloadfile(str + ".pdf");
            //context.Response.Write(str + ".pdf");
        }    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");
                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.Start();
                System.Threading.Thread.Sleep(800);
                p.WaitForExit();
                return true;
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex);
            }
            return false;
        }    public void downloadfile(string s_fileName)
        {        HttpContext.Current.Response.ContentType = "application/ms-download";
            string s_path = System.Web.HttpContext.Current.Server.MapPath("~/PDF/") + 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());        //Response.WriteFile("");        HttpContext.Current.Response.WriteFile(file.FullName);        HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.End();
        }
        
        public bool IsReusable {
            get {
                return false;
            }
        }}
                            <script>
                            //window.location.href
                             $(function(){
                              $(".btnPdf").click(function(){
                              
                              var url=window.location.href;
                                //location.href = "action/PDF.ashx?url="+window.location.href;
                                $.post("action/PDF.ashx",{url:url});
                              });
                             });
                            </script>这个为什么不执行下载啊。。
      

  6.   

    你这样的代码有意义没?下载是不需要用ajax的,除非需要特殊处理的时候
    文件流输出
                DownAdress = dimm + DownAdress;
                if (!File.Exists(Server.MapPath(DownAdress)))
                {
                    Response.Write("下载的文件不存在!");
                    Response.End();
                    return;
                }
                using (FileStream fso = new FileStream(Server.MapPath(DownAdress), FileMode.Open,FileAccess.ReadWrite,FileShare.Inheritable))
                {
                    string[] filename=DownAdress.Split(new char[]{'/'});
                    int len = Convert.ToInt32(fso.Length);
                    byte[] FileObj = new byte[len];
                    fso.Read(FileObj, 0, len);
                    Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", HttpUtility.UrlEncode(filename[filename.Length - 1]), System.Text.Encoding.UTF8));
                    Response.AddHeader("Content-Length", len.ToString());
                    Response.ContentType = "application/octet-stream";
                    Response.Charset = "UTF-8";
                    Response.ContentEncoding = System.Text.Encoding.UTF8; 
                    Response.BinaryWrite(FileObj);
                    Response.Flush();
                    Response.Clear();
                    fso.Close();
                }