none
请教Response.AddHeader问题 RRS feed

  • 问题

  • Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi.Name));

    这句应该是执行下载,但是为什么在默写浏览器没法弹出下载,请问有什么好的解决办法!   谢谢

            int columcount = 9;
            string jswjdc = "" + System.DateTime.Today.Year.ToString() + "-" + System.DateTime.Today.Month.ToString() + "-" + System.DateTime.Today.Day.ToString();
            string strFileToOrg = "", strBufferLine = "", strBufferLine1 = "";
            int i;
            strFileToOrg = HttpContext.Current.Server.MapPath("csv\\mt" + jswjdc + ".csv");
            StreamWriter strmWriterObj = new StreamWriter(strFileToOrg, false, System.Text.Encoding.Default);//声明写入流对象            
            SqlConnection OleDbConnection1 = CC.GetConnection();
            OleDbConnection1.Open();
            SqlCommand cmdGenFile = new SqlCommand();
            cmdGenFile.Connection = OleDbConnection1;
            cmdGenFile.CommandText = "select send_way,send_sn,send_mobile,send_content,send_tongdao,send_dsdate,send_date,send_zhuangt,send_belonging from sms_sendlog  where send_zhuangt='待审核' and send_way='mt'";
            SqlDataReader drGenFile = cmdGenFile.ExecuteReader();
            strmWriterObj.WriteLine("\"编号\",\"用户名\",\"手机\",\"座机\",\"生日\",\"工龄\",\"状态\",\"工号\",\"归属\"");
            while (drGenFile.Read())
            {
                strBufferLine = "";
                strBufferLine1 = "\"" + Convert.ToString(drGenFile.GetValue(0)) + "\"";
                strBufferLine = strBufferLine1;
                for (i = 1; i <= (columcount - 1); i++)
                {
                    strBufferLine1 = "";
                    strBufferLine1 = "\"" + Convert.ToString(drGenFile.GetValue(i)) + "\"";
                    strBufferLine = strBufferLine + "," + strBufferLine1;
                }
                strmWriterObj.WriteLine(strBufferLine);
            }
            strmWriterObj.Close();
            drGenFile.Close();
            OleDbConnection1.Close();
    
            string path = Server.MapPath("csv\\mt" + jswjdc + ".csv");
            FileInfo fi = new FileInfo(path);
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi.Name));
            Response.AddHeader("Content-Length", fi.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.Filter.Close();
            Response.WriteFile(fi.FullName);
            Response.Flush();
            Response.End();
    
     



    • 已编辑 yyyukai 2012年1月6日 9:20
    2012年1月6日 9:04

答案

全部回复