Francesco
Francesco

Reputation: 10820

Download pdf file with JQuery/JSON from extern Web Service

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

Answers (2)

themarcuz
themarcuz

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

Stelian Matei
Stelian Matei

Reputation: 11623

I would choose the following method:

  1. client clicks on a link in the table which points to a url containing the id of the requested pdf (e.g. download?id=3)
  2. on the server side make the request to the third party and then return it to the user directly as bytes. You should also add appropriate headers if you want to force the browser to prompt Open/Save dialog and not opening the file automatically 2.

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

Related Questions