我正在从母版页客户端脚本(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发送参数,你不能这样发布它们。
您还可以阅读:从服务器下载文件的最佳方式是什么 https://stackoverflow.com/questions/10912164/what-is-the-best-way-to-download-file-from-server
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)