在ASP里的语法
Response.BinaryWrite s.Read
写成C#方式
Stream.Read ( NumBytes )
里面的参数应该怎么写呢?我是想把一个文件输出到客户端下载,可是找到的都是ASP的例子,有人能帮助我吗
Response.BinaryWrite s.Read
写成C#方式
Stream.Read ( NumBytes )
里面的参数应该怎么写呢?我是想把一个文件输出到客户端下载,可是找到的都是ASP的例子,有人能帮助我吗
解决方案 »
- 有个C#自动更新缓存问题,谁帮帮忙
- c#链接数据库会不会太频繁
- 急:求助,如何实现"上传文件"只是局部刷新
- C#得到最後一個id 的值
- where 语句 多条件查询格式
- 一个奇怪的数据库问题?
- Assembly对象能否保存成文件
- SqlDataAdapter 的 InsertCommand,DeleteCommand,UpdateCommand怎么用
- 谁能开发这个游戏?
- 用C#做网站是否还需要其他的工具组合开发呢(比如Frontpage等)???????来者有分
- 做一个控制台程序里执行一些数据库操作,然后需要在控制台上显示执行信息,但是这些数据库操作需要每天定时执行,本来想做windows服务,
- 取布尔型字段并显示的问题
[
In,
Out
] byte[] buffer,
int offset,
int count
);buffer
字节数组。此方法返回时,该缓冲区包含指定的字符数组,该数组的 offset 和 (offset + count-1) 之间的值由从当前源中读取的字节替换。
offset
buffer 中的从零开始的字节偏移量,从此处开始存储从当前流中读取的数据。
count
要从当前流中最多读取的字节数。using System;
using System.IO;public class Block
{
public static void Main()
{
Stream s = new MemoryStream();
for (int i=0; i<100; i++)
s.WriteByte((byte)i);
s.Position = 0;
// Now read s into a byte buffer.
byte[] bytes = new byte[1000];
int numBytesToRead = (int) s.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = s.Read(bytes, numBytesRead, numBytesToRead);
// The end of the file is reached.
if (n==0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
s.Close();
// numBytesToRead should be 0 now, and numBytesRead should
// equal 100.
Console.WriteLine("number of bytes read: "+numBytesRead);
}
}
StreamReader将一个Stream包含在内,提供了很多方便的对Stream读取的操作。
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = s.Read(bytes, numBytesRead, numBytesToRead);
// The end of the file is reached.
if (n==0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
-------------------------------------------------------------------------
这一段是干什么的呢?如果仅仅将stream读入bytes,用一行s.Read(bytes,numBytesRead,numBytesToRead)不就可以了?我写的:---------------------------
byte[] bytes = new byte[100];
int numBytesToRead = (int) s.Length;
int numBytesRead = 0;
int n=s.Read(bytes,numBytesRead,numBytesToRead);
Console.WriteLine(n);
for(int i=0;i<(int)s.Length;i++)
Console.WriteLine((int)bytes[i]);
----------------------------------
最终输出的结果是:
10
0
1
2
3
4
5
6
7
8
9
_-------------------------------------------------------
说明stream读入byte〔〕是成功的。
为什么还要用循环呢?一定是我有什么考虑不周的地方,能解释解释吗?