如何在rest WCF服务中上传PDF文件和其他字符串参数

本文关键字:文件 PDF 其他 参数 字符串 rest WCF 服务 | 更新日期: 2023-09-27 17:49:18

我已经创建了一个rest wcf服务,现在我想将pdf文件和其他一些字符串数据发布到它,所以正确的方法是什么

1)将PDF转换为base64字符串,然后形成XML并发布到rest web service,在rest service中解码。

2)使用以下代码直接上传

     byte[] pdfFile = File.ReadAllBytes("pdf file path here");WebRequest request = WebRequest.Create("https://test.site.fr/Testfile");
    request.Method = "POST";
    request.ContentLength = pdfFile.Length;
    request.ContentType = "application/pdf";
    Stream stream = request.GetRequestStream();
    stream.Write(pdfFile, 0, pdfFile.Length);
    stream.Close();
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());
    Console.WriteLine(reader.ReadToEnd());
    reader.Close();

对于大文件来说是更好的,如果需要任何其他压缩

如何在rest WCF服务中上传PDF文件和其他字符串参数

最好的方法是使用multipart/form-data。c#没有内置的多部分/表单-数据解析器,所以请使用c#社区开发人员编写的解析器。

HttP多部分数据解析器