jquery 能执行下载吗

u010072032 2013-05-13 01:32:15

$.post("action/PDF.ashx",{url:url},function(result){
// alert(result);
window.location.href = "PDF/" + result;

hideBlock();
});

window.location.href = "PDF/" + result; 执行到这里他就直接在网页中打开pdf,我想让他执行到这里然后显示下载或另存为,能实现吗,请高手指教。。。
...全文
154 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
md5e 2013-05-15
  • 打赏
  • 举报
回复
引用 6 楼 u010072032 的回复:
<%@ 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> 这个为什么不执行下载啊。。
你这样的代码有意义没? 下载是不需要用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(); }
IssacChow 2013-05-15
  • 打赏
  • 举报
回复
传说中可以用a标签做出下载功能,你试过么?
u010072032 2013-05-15
  • 打赏
  • 举报
回复
帮帮忙啊解决一下啊。 。
xuan.ye 2013-05-15
  • 打赏
  • 举报
回复
流和数据的区别,js常识吗
u010072032 2013-05-15
  • 打赏
  • 举报
回复
u010072032 2013-05-14
  • 打赏
  • 举报
回复
u010072032 2013-05-14
  • 打赏
  • 举报
回复
<%@ 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> 这个为什么不执行下载啊。。
  • 打赏
  • 举报
回复
你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();
[/quote]
u010072032 2013-05-13
  • 打赏
  • 举报
回复
u010072032 2013-05-13
  • 打赏
  • 举报
回复
    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里面判断然后才去执行下载。。。
u010072032 2013-05-13
  • 打赏
  • 举报
回复
引用 1 楼 mylady 的回复:
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();
jquery 里面怎么写啊。高手、、
mylady 2013-05-13
  • 打赏
  • 举报
回复
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();

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧