62,041
社区成员
发帖
与我相关
我的任务
分享
<%@ 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>
这个为什么不执行下载啊。。
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]
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里面判断然后才去执行下载。。。
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();