Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

asp.net - file download by calling .ashx page

I'm requesting .ashx page from Master page client side script (Jquery) which has a code to download a PDF file. When I debug it, I can see the execution of "file download" code but file is not downloading.

$.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();
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your file is downloading, but you get it on javascript, on the data parameter of your call because you call it with Ajax.

You use a handler - so ajax not needed here, and the most easy thing to do using javascript is that:

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

or with a simple link as

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

Ah, and send the parameters via the url, you can not post them that way.

You can also read: What is the best way to download file from server


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...