jquery 能执行下载吗 $.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 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 里面怎么写啊。高手、、 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里面判断然后才去执行下载 你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(); <%@ 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(); } MVC2.0 的URL路由怎么弄啊 弄了半天没有反应 新手 [C#]如何通过aspx页面生成excel文件? 由asp.net做的网站,点击链接有延迟现象? componentart grid,在CALLBACK模式下如何获取当前选中行的某个字段值? 中英文版 IIS的调试权限 怎么控制框架??帮忙解决。感谢。 80分求html语言问题,中饭吃好了结帐 一个建小型查询网站问题 ajax timer控件的刷新问题 js 调用Session值 asp.net mvc 中怎么像webform一样自定义一个BaseController实现判断用户是否登录
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 里面怎么写啊。高手、、
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();
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>这个为什么不执行下载啊。。
文件流输出
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();
}