C# How to convert MessageBodyStream to MemoryStream?
通过WCF服务从数据库取文档数据时,返回的是Stream对象,在DevExpress的PDFViewer显示时,用PDFViewer.LoadDocunent(Stream stream);方法时,报无法从MessageBodyStream转为Stream 的错误。
最后在StackOverFlow上找到了答案。
不能通过转型,只能通过从Stream中读出数据,然后写入到MemoryStream的方法。
I am returning a Stream from a WCF Service and trying to convert it to a MemoryStream.But in the web application where in consume the WCF service,I am getting the result as of "MessageBodyStream" where i was expecting "System.IO.Stream". How can i convert this to a MemoryStream ? |
下面是采纳的回答:
Sometimes the streams come in and you dont know how big they are, for that use this code:
public static byte[] ReadToEnd(System.IO.Stream stream)
{
long originalPosition = stream.Position; byte[] readBuffer = new byte[]; int totalBytesRead = ;
int bytesRead; while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > )
{
totalBytesRead += bytesRead; if (totalBytesRead == readBuffer.Length)
{
int nextByte = stream.ReadByte();
if (nextByte != -)
{
byte[] temp = new byte[readBuffer.Length * ];
Buffer.BlockCopy(readBuffer, , temp, , readBuffer.Length);
Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
readBuffer = temp;
totalBytesRead++;
}
}
} byte[] buffer = readBuffer;
if (readBuffer.Length != totalBytesRead)
{
buffer = new byte[totalBytesRead];
Buffer.BlockCopy(readBuffer, , buffer, , totalBytesRead);
}
return buffer;
}
Then once you have the byte array you can convert it to a memory stream...
byte[] myBytes = ReadToEnd(theStream);
Stream theMemStream = new MemoryStream(myBytes, , myBytes.Length);
C# How to convert MessageBodyStream to MemoryStream?的更多相关文章
- csharp:Convert Image to Base64 String and Base64 String to Image
/// <summary> /// 图像转成二进制数组 /// </summary> /// <param name="imageIn">< ...
- WPF 微信 MVVM 【续】修复部分用户无法获取列表
看过我WPF 微信 MVVM这篇文章的朋友,应该知道我里面提到了我有一个小号是无法获取列表的,始终也没找到原因. 前两天经过GitHub上h4dex大神的指导,知道了原因,是因为微信在登录以后,web ...
- C# base 64图片编码解码
使用WinForm实现了图片base64编码解码的 效果图: 示例base 64编码字符串: /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKD ...
- C# base64 Img 互转
[AcceptVerbs(HttpVerbs.Post)] public JsonResult Upload(HttpPostedFileBase fileData) { try { if (file ...
- canvas生成图片并保存到本地文件夹主要代码
js var url = canvas.toDataURL();//把canvas中的图片变成data:image C# string filepath = ""; string ...
- base64和图片的转换
/// <summary> /// base64转图片 /// </summary> /// <param name="strBase64">& ...
- UWP/Win10新特性系列—Drag&Drop 拖动打开文件
在Win10 App开发中,微软新增了系统PC文件与UWP 之间的文件拖拽行为,它支持将系统磁盘上的文件以拖拽的形式拖入App中并处理,在前不久的微软build 2015开发者大会上微软展示的UWP版 ...
- 中转Http请求
应用场景:公司与外部公司数据对接,外部公司需申请指定IP访问.而本地ip经常变动,无法因ip变动时刻向外部公司申请绑定IP,给本地程序调试带来麻烦,故只能在指定ip服务器上搭建请求中转http请求: ...
- 使用html2canvas实现批量生成条形码
/*前台代码*/ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Generat ...
随机推荐
- jquery-pjax使用说明
pjax = pushState + ajax .--. / \ ## a a ( '._) |'-- | _.\___/_ ___pjax___ ."\> \Y/|<'. '. ...
- [POI2005]SKA-Piggy Banks tarjan 水题
Code: #include<bits/stdc++.h> #define maxn 1000002 using namespace std; void setIO(string s) { ...
- eas之如何获取当前用户
在UI扩展类中,获取当前用户可通过工具类SysContext如:SysContext.getSysContext().getCurrentUserInfo()在ControlerBean的扩展类中,获 ...
- S-HR二开基础
检测是否某个类部署上去了:http://10.3.0.115:6888/easportal/tools/getclassurl.jsp?className=com.kingdee.eas.hr.ser ...
- bzoj4320 homework 题解
题面链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4320 令M=sqrt(mx),把询问的Y按M 分成两种不同的处理方式. 1.对于> ...
- 阿里云服务器centos7环境下安装xampp后,mysql有进程,但是却没有监听3306端口
配置阿里云服务器centos7中xampp环境时出现了一种情况: 在centos中可以使用命令进入mysql 修改后也可以远程用phpmyadmin连接mysql 但是用navicat却无法连接 先修 ...
- 在LinuxMint19上安装搜狗拼音输入法
写在前面 由于Linux mint是基于Ubuntu的深度改造,所以按照网上针对Ubuntu的安装方法基本都是有用的.LinuxMint自身就携带了IBUS和fcitx两个框架.然而并非每次都能正常使 ...
- C#中三种弹出信息窗口的方式
弹出信息框,是浏览器客户端的事件.服务器没有弹出信息框的功能. 方法一: asp.net页面如果需要弹出信息框,则需要在前台页面上注册一个javascript脚本,使用alert方法.使用Client ...
- opencart的语言包安装
语言包复制到对应的目录: 前台:将档案上传至商店根目录\catalog\language\ 后台:将档案上传至商店根目录\admin\language\ 在后台: 增加了中文语言包 ========= ...
- 苦酒入喉心作痛,红酒入鹅鹅想哭——震惊!勒索病毒想哭靠wine感染了Ubuntu16.04
都说linux能免疫win的病毒来着…… 今天作死,逛贴吧时好奇下载了一个想哭2.0 (http://pan.baidu.com/share/link?shareid=1393367320&u ...