I based some code in an ASP.NET MVC app to generate and download a PDF file on some working code from an ASP.NET Web API app that downloads Excel files.
In the working (Web API/Excel) code, when I create this html (link) on the page:
builder.Append(string.Format("<a href=\"api/deliveryperformance/excel/{0}/{1}/{2}\">{3}</a>", unit, startDateYYYYMMDD, endDateYYYYMMDD, fileBaseName));
...and the user clicks it, this Controller is called:
[Route("{unit}/{begindate}/{enddate}")]
public HttpResponseMessage Get(string unit, string begindate, string enddate)
{
byte[] excelContents;
string selectStmt = "SELECT BinaryData FROM ReportsGenerated WHERE FileBaseName = @fileBaseName";
string fbn = string.Format("deliveryperformance/{0}/{1}/{2}", unit, begindate, enddate);
using (SqlConnection connection = new SqlConnection(PlatypusWebReportsConstsAndUtils.CPSConnStr))
using (SqlCommand cmdSelect = new SqlCommand(selectStmt, connection))
{
cmdSelect.Parameters.Add("@fileBaseName", SqlDbType.VarChar).Value = fbn;
connection.Open();
excelContents = (byte[])cmdSelect.ExecuteScalar();
connection.Close();
}
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(excelContents)
};
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = string.Format("{0}.xlsx", fbn)
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
...which successfully downloads an Excel file to the user's machine.
So, I've got code that is as-similar-as-possible in my MVC app to generate and download a PDF file like so:
internal static HttpResponseMessage GeneratePDFOfReportFutures()
{
HttpResponseMessage result = null;
futureReports = GetAllFutureReports();
try
{
using (var ms = new MemoryStream())
{
using (var doc = new Document(PageSize.A4.Rotate(), 25, 25, 25, 25))
{
using (PdfWriter.GetInstance(doc, ms))
{
//Open the document for writing
doc.Open();
. . .
doc.Add(titleTable);
. . .
doc.Add(tblHeadings);
. . .
doc.Add(tblRow);
} // foreach
doc.Close();
var bytes = ms.ToArray();
result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(bytes)
};
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = string.Format("{0}.pdf", "test")
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
} // pdfWriter
} // doc
} // memoryStream
} // try
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return result;
} // GeneratePDFOfReportFutures
...which I call from the client via an Ajax call:
$("#btnViewList").click(function () {
$.ajax({
type: "GET",
url: '@Url.Action("GeneratePDF", "GenerateScheduledRptsPDF")',
success: function () {
alert('success from btnViewList');
},
error: function () {
alert('error in btnViewList');
}
});
});
After working through some issues detailed here I am to the point where the Controller method runs without throwing an exception (and I see the success msg ("success from btnViewList")); however, no (PDF) file is downloaded/written to the hard disk.
Why not?