Download a file by calling the .ashx page

I am requesting an .ashx page from the client side of the main page of the script (jQuery), which has the code to download the PDF file. When I debug it, I see the file upload code execution, but the file does not load.

$.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(); } 
+8
jquery ashx
source share
1 answer

Your file is uploaded, but you get it in javascript, in the data parameter of your call, because you are calling it using Ajax.

You use a handler - therefore ajax is not needed here, and the simplest thing used with javascript is this:

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

or with a simple link like

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

Ah and send the parameters via url, you cannot publish them that way.

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

+11
source share

All Articles