如何将PDF文档附加到SOAP并将其从windows客户端发送到C#net中的WCF服务器?
我需要附加一个PDF文档并将其发送到WCF服务器,该服务器将提取一些特定的细节并将响应发送回客户端。请建议我如何在C#.Net中实现这一点。您可以在客户端使用以下方法将文件编码为base 64:如何将PDF文档附加到SOAP并将其从windows客户端发送到C#net中的WCF服务器?,c#,.net,visual-studio-2012,C#,.net,Visual Studio 2012,我需要附加一个PDF文档并将其发送到WCF服务器,该服务器将提取一些特定的细节并将响应发送回客户端。请建议我如何在C#.Net中实现这一点。您可以在客户端使用以下方法将文件编码为base 64: private string EncodeFileAsBase64(string inputFileName) { byte[] binaryData; using (System.IO.FileStream inFile = new System.IO.File
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类中实现此接口:
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文件中提取具体细节并返回回复?很好,很高兴知道它对你有用。请将此标记为您问题的答案。谢谢:)