读取pdf文件作为memorystream或bytearray [英] reading pdf file as memorystream or bytearray

查看:235
本文介绍了读取pdf文件作为memorystream或bytearray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务:WCF REST服务(.Net Framework 4.0)

Service : WCF REST Service (.Net Framework 4.0)

平台:64位(Windows)

Platform: 64-bit (Windows)

我在节点js(javascript)中有一个客户端应用程序,该客户端应用程序与WCF Rest服务进行通信以获取数据并将其显示在屏幕上.我正在尝试通过此服务读取PDF文件,并在客户端计算机上打开PDF,但无法这样做 到目前为止.需要您的帮助来实现相同目标.下面是代码: 

I have a client application in node js (javascript) 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服务方法合同定义

WCF Service method contract definition

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

WCF服务端代码

WCF Service Side Code

        public MemoryStream GetPDF(string empid)
        {
            string pdfName = "myfile.pdf";
            byte[] bytes = File.ReadAllBytes(@"C:\working\myfile.pdf");
            /*
             * Also tried below to read the bytes :-
             * byte[] bytes = Encoding.UTF8.GetBytes(@"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; 
        }

当我尝试直接在浏览器中调用此方法时,会引发如下错误: 

客户端Javascript代码

Client Side Javascript Code

const shell = require('electron').shell

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);
        shell.openItem(data);
    })

});

request.end();

在尝试打开服务返回的数据时,我也遇到与上述屏幕截图相同的错误.

Here also i get the same error as above screenshot when it tries to open the data returned by the service.

请帮助我解决问题.预先感谢.

Please help me to resolve the issue. Thanks in advance.

推荐答案

我的帖子对所有人可见吗?
is my post visible to everyone ?


这篇关于读取pdf文件作为memorystream或bytearray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆