Question WCF Restful service cannot convert PDF file as MemoryStream or ByteArray

hi_shobhit82

New member
Joined
Nov 3, 2016
Messages
1
Location
Pune, Maharashtra, India
Programming Experience
5-10
Service : WCF REST Service (.Net Framework 4.0)
Platform: 64-bit (Windows)

Hi,

I have a WCF rest service to fetch data and show it on the client screen. I am using a node js client application which mostly uses javascript to fetch this pdf memory stream and then displays it onto the client machine.

I am trying to read PDF files in the wcf restful service and send it as a memory stream and open the PDF on the client machine and haven't been able to do so by far. Need your help in achieving the same. Below is the code :


WCF Service method contract definition

C#:
[COLOR=#303336][FONT=Consolas][[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]OperationContract[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]]
[/FONT][/COLOR][COLOR=#303336][FONT=Consolas][[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]WebGet[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]([/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]ResponseFormat[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]=[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]WebMessageFormat[/FONT][/COLOR][COLOR=#303336][FONT=Consolas].[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]Json[/FONT][/COLOR][COLOR=#303336][FONT=Consolas])]
[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas] MemoryStream  [/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]GetPDF[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]([/FONT][/COLOR][COLOR=#101094][FONT=Consolas]string[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] empid[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]);
[/FONT][/COLOR]

WCF Service Side Code

C#:
[COLOR=#101094]    public [/COLOR][COLOR=#2B91AF]MemoryStream  [/COLOR][COLOR=#2B91AF]GetPDF[/COLOR][COLOR=#303336]([/COLOR][COLOR=#101094]string[/COLOR][COLOR=#303336] empid[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#303336]{[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#101094]string[/COLOR][COLOR=#303336] pdfName [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]"myfile.pdf"[/COLOR][COLOR=#303336];[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#101094]byte[/COLOR][COLOR=#303336][][/COLOR][COLOR=#303336] bytes [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#2B91AF]File[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]ReadAllBytes[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]@"C:\working\myfile.pdf"[/COLOR][COLOR=#303336]);[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#858C93]/*
         have also tried the below code for reading the bytes with different encoding :-
               Encoding.UTF8.GetBytes(@"C:\working\myfile.pdf") 
         but even that doesn't work
        */[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#2B91AF]MemoryStream[/COLOR][COLOR=#303336] ms [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#101094]new [/COLOR][COLOR=#2B91AF]MemoryStream[/COLOR][COLOR=#303336]();[/COLOR][COLOR=#303336]
        ms[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Write[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]bytes[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7D2727]0[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] bytes[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Length[/COLOR][COLOR=#303336]-[/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336]);[/COLOR][COLOR=#303336]
        ms[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Position[/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]0[/COLOR][COLOR=#303336];[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#2B91AF]WebOperationContext[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Current[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]OutgoingResponse[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]ContentType[/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]"application/pdf"[/COLOR][COLOR=#303336];[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#2B91AF]WebOperationContext[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Current[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]OutgoingResponse[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Headers[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Add[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Content-disposition"[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7D2727]"inline; filename="[/COLOR][COLOR=#303336]+[/COLOR][COLOR=#303336] pdfName[/COLOR][COLOR=#303336]);[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#101094]return[/COLOR][COLOR=#303336] ms[/COLOR][COLOR=#303336];[/COLOR][COLOR=#858C93]//ms.Length = 4658228
[/COLOR][COLOR=#303336]}
[/COLOR]
When I am trying to call this method directly into the browser with the help of the URL it throws an error like below instead it should open the PDF into the browser:
w5aFa.jpg

Not sure if that should be the correct behaviour. Could you please let me know whats going wrong here.
 
Back
Top Bottom