日期:2014-05-17 浏览次数:20479 次
if (!Directory.Exists(Server.MapPath("UserTemp"))) { Directory.CreateDirectory(Server.MapPath("UserTemp")); } string path = Path.Combine("UserTemp//", "白名单导出_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt"); string str5 = base.Server.MapPath(path); if (File.Exists(str5)) { File.Delete(str5); } //数据量大用另外一种方法 FileInfo file = new FileInfo(str5);//用于获取文件信息 StreamWriter textFile = null; textFile = file.CreateText(); string sqlWhile = "SELECT WMobile FROM WMobile"; DataTable tab = new DataTable(); tab = DBCommonClass.DbAccessHelper.ExecuteDataTable(new DbCommandData(sqlWhile)); string strWhile = string.Empty; for (int i = 0; i < tab.Rows.Count; i++) { string str = tab.Rows[i][0].ToString(); strWhile += str + "\r\n"; } textFile.Write(strWhile); textFile.Close(); ///////////////////////////////////////////////////下面就是下载代码了 //Response.Clear();//清空输出 //Response.Charset = "GB2312";//设定编码 //Response.ContentEncoding = System.Text.Encoding.UTF8; //Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(file.Name)); //Response.AddHeader("Content-length", file.Length.ToString()); //Response.ContentType = "application/octet-stream"; ////ms-txt"; //Response.WriteFile(file.FullName); //Response.End(); Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(file.Name, Encoding.UTF8)); Response.AddHeader("Content-Length", file.Length.ToString()); Response.AddHeader("Content-Transfer-Encoding", "binary"); Response.ContentType = "application/octet-stream"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.WriteFile(file.FullName); Response.Flush(); HttpContext.Current.ApplicationInstance.CompleteRequest();