加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

接受并返回C#WebService的文件

发布时间:2020-12-16 07:29:38 所属栏目:百科 来源:网络整理
导读:如何在C#中创建一个WebService,它将接受File,然后在一次调用中同时返回一个File(Synchronous). 我要做的是创建一个接受和MS Office文档的WebService,将该文档转换为PDF然后将该文件返回给调用者(在我的情况下,我使用 Java作为客户端) 解决方法 正如silvermin
如何在C#中创建一个WebService,它将接受File,然后在一次调用中同时返回一个File(Synchronous).
我要做的是创建一个接受和MS Office文档的WebService,将该文档转换为PDF然后将该文件返回给调用者(在我的情况下,我使用 Java作为客户端)

解决方法

正如silvermind在他的评论中所说,最好的选择是在你的webservice中接受并返回一个字节数组.

您可以使用如下方法将文件作为bytearray加载:

public byte[] FileToByteArray(string _FileName)
{
    byte[] _Buffer = null;

    try
    {
        System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
        System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream);
        long _TotalBytes = new System.IO.FileInfo(_FileName).Length;
        _Buffer = _BinaryReader.ReadBytes((Int32)_TotalBytes);
        _FileStream.Close();
        _FileStream.Dispose();
        _BinaryReader.Close();
    }
    catch (Exception _Exception)
    {
        Console.WriteLine("Exception caught in process: {0}",_Exception.ToString());
    }
    return _Buffer;
}

此外,如果您已将Web服务实现为WCF服务,则可能需要调整一些设置以增加可以发送的信息数量和超时.这是允许这种情况的绑定配置示例. (只有一个样本,可能不符合您的需求)

<binding name="WebServiceBinding" closeTimeout="00:02:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:02:00"
            allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
            maxBufferPoolSize="524288" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
            useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读