C#WCF通过流发送文件 [英] C# WCF Sending a file through streams

查看:142
本文介绍了C#WCF通过流发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要使用WCF服务使用流发送文件.尝试获取流的长度时出现错误.
在通过流的服务中,我发现流属性(例如流长度等)不允许.

我收到以下消息不支持指定的方法,SystemNotSupportedException".我传递给服务的流是转换为行Stream的FileStream,因为我需要读取文件,并且WCF仅接受行流.

有人可以找到解决方案吗?

谢谢.

Hi All,

I need to send a file using a stream using WCF services. I got an error when trying to get the length of the stream.
At the service when I pass the stream I found that the stream properties like stream length,etc are not allowing.

I got following message "Specified method is not supported, SystemNotSupportedException". The stream I passed to the service is a FileStream casted to row Stream since I need to read a file and WCF only accepts row Streams.

Can someone find a solution for this?

Thanks.

推荐答案

我要做的是将文件的内容读入数组并将其发送给客户端.在这种情况下,您可能需要包括文件类型,更好的是,您可以为其创建包装器

what I would do is read the content of the file in to an array and send that to the client. in that case you would need to include the file type probably, better yet you can create a wrapper for it

[Serializable]
       class Filewrapper
       {
           public byte[] FileContent { get; private set; }
           public string FileName { get; private set; }
           public Filewrapper()
           { }
           /// <summary>
           ///
           /// </summary>
           /// <param name="content"></param>
           /// <param name="name">only the file name not the full path</param>
           public Filewrapper(byte[] content, string name)
           {
               FileContent = content;
               FileName = name;
           }
       }


那么您的服务将返回类似这样的内容


then your service would return something like this

string strpath="file path";
Filewrapper fl = new Filewrapper(File.ReadAllBytes(strpath),Path.GetFileName(strpath));



希望对您有帮助



hope this helps


这篇关于C#WCF通过流发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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