我正在从母版页客户端脚本 (Jquery) 请求 .ashx 页面,该脚本具有下载 PDF 文件的代码。当我调试它时,我可以看到“文件下载”代码的执行,但文件没有下载。
$.ajax({
type: "POST",
url: "FileDownload.ashx",
dataType: "html",
success: function (data) { }
} );
public class FileDownload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string fileName = "BUSProjectCard.pdf";
string filePath = context.Server.MapPath("~/Print/");
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-Disposition", "attachment; filename="+fileName);
context.Response.TransmitFile(filePath + fileName);
context.Response.End();
}
最佳答案
您的文件正在下载,但您通过 javascript 在调用的 data
参数上获取它,因为您使用 Ajax 调用它。
您使用处理程序 - 因此这里不需要 ajax,使用 javascript 做的最简单的事情是:
window.location = "FileDownload.ashx?parametres=22";
或者使用简单的链接
<a target="_blank" href="FileDownload.ashx?parametres=22" >download...</a>
啊,通过url发送参数,不能这样post。
关于asp.net - 通过调用.ashx页面下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12087040/