Sending pdf between WCF rest service and electron


#1

Node Version: 5.7.1

Service : WCF REST Service (.Net Framework 4.0)

Platform: 64-bit (Windows)

Hi,

I have an electron application which communicates with a WCF rest service to fetch data and show it on the screen. I am trying to read PDF files through this service 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

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
MemoryStream GetPDF(string empid);

WCF Service Side Code

public MemoryStream GetPDF(string empid)
{
    string pdfName = "myfile.pdf";
    byte[] bytes = File.ReadAllBytes(@"C:\working\myfile.pdf");
    MemoryStream ms = new MemoryStream();
    ms.Write(bytes, 0, bytes.Length - 1);
    ms.Position = 0;
    WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
    WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-disposition", "inline; filename=" + pdfName);
    return ms; //ms.Length = 4658228
}

When I am trying to call this method directly into the browser it throws an error like below :

Not sure if that should be the correct behaviour.

Now I am trying to connect to this service through electron client.

Client Side Electron App Code

    var optionsGetPDF = {
        host: 'localhost',
        port: 8080,
        path: '/GetPDF?empid=' + empid.trim(),
        method: 'GET'
    };

var request = http.request(optionsGetPDF, function (response) {
    var data = [];
    response.on('data', function (chunk) {
        data.push(chunk);
    })
    response.on('end', function () {
        data = Buffer.concat(data);
        alert(data.length); // 16573708 bytes
    })
});

request.end();

I also tried using a memory stream on the client side but that didn’t work either.

Client Side Alternate Code

    var memStream = new memorystream(null, { readable: false });
    var request = http.request(optionsGetPDF, function(response) {
        response.pipe(memStream);            
        response.on('end', function() {                
            alert(memStream.toString().length); // 16573708 bytes
        })
    });
    request.end();

In both the cases the number of bytes are more than what i am sending from the service i.e. 4658228 bytes , but what i recieve instead is 16573708.

Please advise some expert help of yours to resolve the issue.


#2

This is a duplicate of your previous topic:

We like to unify similar discussions under one topic so that people don’t have to read a bunch of different threads to get the whole story. Please continue the discussion there. I’ll be closing this topic as duplicate.


#3

This topic was automatically closed after 12 hours. New replies are no longer allowed.