When accessing the URL directly from my program that generates a PDF, I am able to initiate a direct download.
public void Download(byte[] file)
{
try
{
MemoryStream mstream = new MemoryStream(file);
long dataLengthToRead = mstream.Length;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.BufferOutput = true;
Response.ContentType = "Application/pdf"; /// if it is text or xml
Response.AddHeader("Content-Disposition", "attachment; filename=" + "Test.pdf");
Response.AddHeader("Content-Length", dataLengthToRead.ToString());
mstream.WriteTo(Response.OutputStream);
Response.Flush();
Response.Close();
HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
HttpContext.Current.Response.SuppressContent = true; // Gets or sets a value indicating whether to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
}
catch (Exception e)
{
}
}
However, when performing a POST with my JSON to the URL, this method does not result in a direct download.
The POST request with the JSON string is successful. However, the download does not start after the Ajax call. My program functions as a basic ASPX website that loads all data and functions within the Page_Load event.