如何从字节数组中获取PDF

本文关键字:获取 PDF 数组 字节数 字节 | 更新日期: 2023-09-27 18:17:41

我想做的是…

让用户使用WCF选择PDF文件和其他一些文件,一旦他们选择了文件,这些文件需要从那里移动到远程服务器(公司托管)。我试图对数据进行反序列化(将其读取为字节)并将其传输过去。我创建了一个表单,只是为了测试目的,看看我的客户端是如何工作的。

当我得到文件....我可以显示文件,但我想要返回实际的PDF,但我做不到。我可以在记事本中打开文件(但它是字节格式),当我试图在PDF中打开它时,它说文件格式不受支持。我真的很困惑,不知道该做什么。

非常感谢您的帮助。

Code Snippet:

客户端:

    private void btnUploadFile_Click(object sender, EventArgs e)
            {
            string pathServer = @"C:'Users'....'Desktop'Test.pdf";
            RestClient newClient = new      RestClient("http://localhost:...../Service1.svc/DisplayRawData");
            var request = new RestRequest(Method.GET);
            request.RequestFormat = DataFormat.Json;
            request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
            IRestResponse<TempString> newResponse = newClient.Execute<TempString>(request);
            //List<TempString> rtrn = (List<TempString>)newResponse.Data;     
            var responseData = newClient.DownloadData(request);
            FileStream fStream = new FileStream(pathServer, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fStream);
            bw.Write(responseData);
            bw.Close();
            foreach (var xbyte in responseData)
            {
               // fStream.WriteByte(xbyte); 
            }
            //fStream.Flush();
            fStream.Close(); 
Server Side (Service)
public string DisplayRawData()
        {
            string path = @"C:'basketball.pdf";
            byte[] fileToSend = File.ReadAllBytes(path);
            string filetoSendB64 = Convert.ToBase64String(fileToSend);
           // WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
            return filetoSendB64;
        }
Interface
        //Getting Stream from a File
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "DisplayRawData",
                    RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        // string DisplayRawData();
        string DisplayRawData();

如何从字节数组中获取PDF

代码有点令人困惑,但我想说,看起来您需要在将其写入文件之前将响应字符串从Base64解码回字节数组。