接受并从C#的WebService返回文件/ [英] Accept and Return file to/from C# WebService

查看:594
本文介绍了接受并从C#的WebService返回文件/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建在C#中一个WebService即会接受文件,然后的收益在一个调用(同步),同时文件。
我试图做的是创造一个WebService,将接受和MS Office文档,该文档转换为PDF,然后返回该文件返回给调用者(在我的情况下,我使用Java客户端)

How do I create a WebService in C# that will accept File and then return a File at the same time in one call (Synchronous). What I'm trying to do is to create a WebService that will accept and MS Office document, convert that document to PDF and then return that file back to the caller (in my case I'm using Java for the client)

推荐答案

由于silvermind在他的评论中说,最好的选择是接受并在你的web服务返回的字节数组。

As silvermind said in his comment, the best option is to accept and return an array of bytes in your webservice.

您可以加载一个文件作为这样一个方法的ByteArray:

You can load a file as a bytearray with a method like this one:

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服务,你可以需要调整一些设置,以增加信息ammount的,你可以发送和超时。这是在绑定配置允许该样品。 (只有一个样本,可能不符合你的需求)

In addition, if you have implemented the webservice as a WCF service, you may need to tweak some settings to increase the ammount of information you can send and the timeout. This is sample of the binding configuration that allow that. (Only a sample, may not match your needs)

 <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>

这篇关于接受并从C#的WebService返回文件/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆