C# 如何将字节流写入缓冲区

来源:百度知道 编辑:UC知道 时间:2024/06/17 02:55:53
代码如下,我从数据库中拿到了存入的word,那么现在如何将这个byte[]类型的wordFile存入缓冲区,发送到客户端呢?
string id = Request.QueryString["id"];
WordInfo wi = new WordInfo();//声明实体类
wi=BLL.WordInfoManager.GetWordInfoByWordId(int.Parse(id));//从数据库获得数据
byte[] wordFile = wi.Wordfile;//把二进制word数据存入byte[]
byte[] buffer = new Byte[10240];//声明一个字节数组
BufferedStream bs = new BufferedStream(wordFile);
int curPos = 0;
int readsize = 0;
long size = bs.Length;
readsize=bs.Read(buffer, 0, 10240);
while (readsize == 10240)
{
curPos += readsize;
Response.BinaryWrite(buffer);
Response.Flush();
readsize = bs.Read(buffer, curPos, 10240);
}
byte[] bfEnd = new byte[readsize];
bs.Read(bfEnd, cu

只要你把二进制存入了内存
那跟生成图片生成普通文件的原理是一样的
用流一点点的循环
生成了文件后
然后下载给客户端即可

response里面设置头信息,content-type : application/msword (大概是这个)
然后再写出去就行了,
也有可能要用base64编码。