指定的参数已超出有效值的范围。
参数名: offset 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
参数名: offset源错误: 
行 29:         //通知浏览器下载文件而不是打开
行 30:         Response.AddHeader("Content-Disposition", "inline;  filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
行 31:         Response.BinaryWrite(bytes);
行 32:         Response.Flush();
行 33:         Response.End();
 源文件: c:\Inetpub\wwwroot\FolderAuth\File.ashx    行: 31 堆栈跟踪: 
[ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
参数名: offset]
   System.Web.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 count) +8858392
   System.Web.HttpResponse.BinaryWrite(Byte[] buffer) +27
   File.ProcessRequest(HttpContext context) in c:\Inetpub\wwwroot\FolderAuth\File.ashx:31
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
 
我用的HyperLink 超链接到当前页file.ashx,file,ashx中的代码如下所示: 
我的源代码是:
HttpResponse Response = context.Response;
        //绝对路径
        string path = HttpContext.Current.Session["chiefPath"].ToString() + "\\" + context.Request.QueryString["path"];        
        //文件名称
        string filename = Path.GetFileName(path);
        string filenameHtml = Path.GetFileNameWithoutExtension(filename)+".htm";
        string fileexten = Path.GetExtension(filename);//文件扩展名
        
        //以字符流的形式下载文件
        FileStream fs = new FileStream(path, FileMode.Open);
        byte[] bytes = new byte[2*(int)fs.Length];
        fs.Read(bytes, 0, bytes.Length);
        fs.Close();       
        Response.ContentType = "application/octet-stream";
        //通知浏览器下载文件而不是打开
        Response.AddHeader("Content-Disposition", "inline;  filename=" + HttpUtility.UrlEncode(filename,    System.Text.Encoding.UTF8));
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();

解决方案 »

  1.   

     Response.BinaryWrite===>Response.WriteFile
      

  2.   

    我首先把bytes转换成string string 
    strbytes = System.Text.Encoding.Default.GetString(bytes);
    再Response.WriteFile(strbytes );提示 访问被拒绝
      

  3.   

    Stream.Write 方法offset: buffer 中的从零开始的字节偏移量,从此处开始将字节复制到当前流。offset 超出范围,说明你给定的参数超出了 byte[] 对象的字节数 (offset < 0 or offset > length)
      

  4.   

    解决,用Response.WriteFile(filename,true);刚才没看明白Response.WriteFile的用法,谢谢
      

  5.   

    将二进制的转换成string,用Stream.Write也行得通,谢谢。 
      

  6.   

    byte[] bytes = new byte[2*(int)fs.Length];
    为什么要乘2。下面是我用过的,string filepath = Server.MapPath("~/Formater/Release.zip");
            
            string sGenName = "ExcelFormater.zip";
            System.IO.FileStream fs = null;
            fs = System.IO.File.Open(filepath, System.IO.FileMode.Open);
            byte[] btFile = new byte[fs.Length];
            fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
            fs.Close();        Response.AddHeader("Content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(sGenName)));
            Response.ContentType = "multipart/x-zip";
            Response.BinaryWrite(btFile);
            Response.End();