如何将 PDF 文档附加到 SOAP 并将其从 windows 客户端发送到 C# .net 中的 WCF 服务器?

How to attach a PDF document to SOAP and send it from windows client to WCF server in C# .net?

我需要附加一个 PDF 文档并将其发送到 WCF 服务器,该服务器将提取一些特定的详细信息并将响应发送回客户端。请建议我如何在 C# .Net 中实现这一点

您可以在客户端使用以下方法将您的文件编码为 base 64:

private string EncodeFileAsBase64(string inputFileName)
    {
        byte[] binaryData;
        using (System.IO.FileStream inFile = new System.IO.FileStream(inputFileName,
                                   System.IO.FileMode.Open,
                                   System.IO.FileAccess.Read))
        {
            binaryData = new Byte[inFile.Length];
            long bytesRead = inFile.Read(binaryData, 0,
                                 (int)inFile.Length);
            inFile.Close();
        }

        // Convert the binary input into Base64 UUEncoded output. 
        string base64String;
        base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
        return base64String;
    }

现在在服务端你可以拥有物理文件,你可以执行任何你想要的操作。这将确保文件在通过网络传输时的安全性。

这将允许您将文件上传到 WCF 服务:

在您的上传服务界面中创建服务合同:

[ServiceContract]
public interface IVO_Upload
{
                [OperationContract]
                string UploadFile(RemoteFileInfo request);
}

[MessageContract]
    public class RemoteFileInfo : IDisposable
    {
        [MessageHeader(MustUnderstand = true)]
        public string FileName;
        [MessageHeader(MustUnderstand = true)]
        public long Length;
        [MessageHeader(MustUnderstand = true)]
        public string Response;

        [MessageBodyMember(Order = 1)]
        public System.IO.Stream FileByteStream;

        public void Dispose()
        {
            if (FileByteStream != null)
            {
                FileByteStream.Close();
                FileByteStream = null;
            }
        }
    }

然后在你的.svc中实现这个接口class:

public class VO_eLoyalty : IVO_Upload
{

                string IVO_Upload.UploadFile(RemoteFileInfo request)
                {
                                string _uploadFolder = "PATH_TO_SAVE_FILE";
                                string filePath = Path.Combine(_uploadFolder, request.FileName);
                                using (targetStream = new FileStream(filePath, FileMode.Create,
                                FileAccess.Write, FileShare.None))
                                {
                                                //read from the input stream in 65000 byte chunks

                                                const int bufferLen = 65000;
                                                byte[] buffer = new byte[bufferLen];
                                                int count = 0;
                                                while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
                                                {
                                                                // save to output stream
                                                                targetStream.Write(buffer, 0, count);
                                                }

                                                targetStream.Close();
                                                sourceStream.Close();

                                }
                                return "details"; // return whatever you need to here, or change the return type to whatever you need

                }
}

我想您对从 PDF 文件中提取具体细节并返回响应感到满意吗?