C#WCF通过流发送文件

编程入门 行业动态 更新时间:2024-10-22 16:34:02
本文介绍了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通过流发送文件

本文发布于:2023-11-25 17:38:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630708.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过流   文件   WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!