Reputation: 10820
I have a web application (ASP.NET/C#) for which I would need to download a pdf file from the Server to Client side by double clicking on a table row.
My idea was to use JQuery/Ajax to call a web service. The web service calls another web service (provided by an external partner) that returns a PDF file (just this format) as byte[].
I have read of several possibilities, where in almost all cases the file is saved on the server and then returned to the client via the HTTPContaxt.Current.Response. In other cases an URL is provided to the client side and then the client is redirected to an hidden iFrame with such an URL.
What is the best approach to deliver a pdf file to the client, so that she/he can have open or save it locally? Thanks.
Upvotes: 0
Views: 3662
Reputation: 2583
Usually I simply call an external page, with a simple javascirpt windows.open("GetAttach.ashx?id=5") and then, in that handler, the code to return the pdf byte array will be
byte[] allegato = GetPdfFromExternalSources;
response.ContentType = System.Net.Mime.MediaTypeNames.Application.Pdf;
response.AppendHeader("content-disposition", string.Format("attachment; filename=\"{0}\"", filename));
response.BufferOutput = false; //Stream the content to the client, no need to cache entire streams in memory...
response.BinaryWrite(allegato);
response.Close();
Upvotes: 1
Reputation: 11623
I would choose the following method:
If the requested PDFs are large or there are many requests for the same PDFs, you can implement a minimum caching system on the server and save the files there for future use.
Upvotes: 0