Click here to Skip to main content
15,891,867 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am calling a webservice(using web client) in mvc code that returns a byte array of pdf doc and store the bytes array in local machine as pdf.The pdf gets generated at desire location but when i open it, it shows file not supported error.So can anyone tell me how to resolve the issue.I also need API code that returns a pdf based on filename passed to it.
Posted
Updated 5-Jun-17 23:41pm
Comments
Sergey Alexandrovich Kryukov 25-Mar-15 15:54pm    
"It shows file not supported error" is not informative, it does not give us clear idea on what you are trying to do. You should not assume that the user has any software to view it.
—SA
F-ES Sitecore 25-Mar-15 17:25pm    
If you're reading the stream using the Read function then chances are you're only reading part of it as streams deliver their data in chunks so what you have written to disc is only part of the file. Either keep using the Read method and appending to the buffer until the entire stream is read

https://msdn.microsoft.com/en-us/library/system.io.stream.read(v=vs.110).aspx

or use a method that reads the stream in entirety (if one exists), or one way is to use File.Create to create a stream that writes to the desired file on your disc, then simply use the CopyTo method of the stream that has come back from the webapi to copy it to the stream you got from FileCreate. That will handle all the chunking for you.

https://msdn.microsoft.com/en-us/library/dd782932(v=vs.110).aspx

I have sample code for this but not on me right now, you can use the above to give it a go or google for a solution, and failing that I can dig the code out when I have it in front of me.
Member 11364465 26-Mar-15 2:21am    
I need to host a api on a server where my pdf files resides. then i call api from my code by passing the file to get the pdf and then i will display it to the browser.How my api will return a pdf n how i can consume in my app is something on which i struck.

use can return FileResult.

C#
private FileResult ViewPDF()
      {

          var pdfByte = <your code="">;
          return File(pdfByte, "application/pdf");
      }</your>


I used this function in Popup(using IFrame) as my requirement was to show pdf in popup.
hope this will help you
 
Share this answer
 
Whatever you tried will fail as the generated attachment or inline fileResult will not be shown as PDF. it will gives you error "file not supported",
I will suggest you to use iTextSharp to generate PDF.

following links could be helpful

http://stackoverflow.com/questions/25164257/how-to-convert-html-to-pdf-using-itextsharp[^]

https://www.simple-talk.com/dotnet/asp.net/asp.net-mvc-action-results-and-pdf-content/[^]
 
Share this answer
 
Your API will return something like

return new FileStreamResult(pdfStream, "application/pdf")


where pdfStream is a stream of your PDF, either from a PDF generator that returns the PDF as an in-memory stream, or a stream from a file if it is on the disk.

If you want to retrieve the PDF from this api and show it in the browser you need to read the stream, then re-write the stream to the client.

string url = "http://yourserver/api/getpdf";

WebRequest req = WebRequest.Create(url);
req.Method = "GET";

req.ContentType = "application/x-www-form-urlencoded";
WebResponse response = req.GetResponse();

dataStream = response.GetResponseStream();

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=test.pdf");
Response.ContentType = "application/pdf";

dataStream.CopyTo(Response.OutputStream);

dataStream.Close();
dataStream.Dispose();
response.Close();
Response.End();


The above is an aspx client page, but the concept for MVC is the same....get the stream from the api and push it to the client response.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900