asp.net – 通过调用.ashx页面下载文件

我正在从主页客户端脚本( 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上获取它,在您的调用的数据参数上,因为您使用Ajax调用它.

你使用一个处理程序 – 所以这里不需要ajax,使用javascript最简单的方法是:

window.location = "FileDownload.ashx?parametres=22";

或者用简单的链接作为

<a target="_blank" href="FileDownload.ashx?parametres=22" >download...</a>

啊,并通过网址发送参数,你不能这样发布.

您还可以阅读:What is the best way to download file from server

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....