asp.net varbinary(MAX) 类型字段,保存任意文件,下载时出错。
代码:
            DataBaseClass db = new DataBaseClass();
            string sSqlText = "select fileContent from T_FileLists  WHERE id=3";
            SqlConnection con = new SqlConnection(db.sConString);
            con.Open();
            SqlCommand cmd = new SqlCommand(sSqlText, con);
            SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            string fileName = "bbb.doc";
            if (dr.Read())
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "utf-8";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName); //把 attachment 改为 online 则在线打开
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
                Response.ContentType = "application/octet-stream";
                Response.BinaryWrite((byte[])dr[0]);
            }
            con.Close(); 出错:

解决方案 »

  1.   

    看你这个方法感觉不是很很,我觉得先把表的内容循环读取出来,再一次输出.你现在是直接循环输出.参考:
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "text/richtext";
    Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8) + ";size=0");
    Response.Write(sb.ToString());
    Response.Flush();
    Response.End();
      

  2.   

    我是先把一个文件保存到sql server里面的一个varbinary(MAX)字段,再实现一个按钮就直接下载该字段内容成文一个文件。