在ASP里的语法
Response.BinaryWrite s.Read  
写成C#方式
Stream.Read ( NumBytes )
里面的参数应该怎么写呢?我是想把一个文件输出到客户端下载,可是找到的都是ASP的例子,有人能帮助我吗

解决方案 »

  1.   

    public abstract int Read(
       [
       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);
        }
    }
      

  2.   

    呵呵,其实你使用StreamReader类就能简化很多工作了。
    StreamReader将一个Stream包含在内,提供了很多方便的对Stream读取的操作。
      

  3.   

    而实际上你想输出一个文件到Response,则可以使用Response.Out,如果我没记错的话,Response.Out正是一个输出流,而Response.Output则是TextWriter。
      

  4.   

    -------------------------------------------------------------------------
    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〔〕是成功的。
    为什么还要用循环呢?一定是我有什么考虑不周的地方,能解释解释吗?