在浏览器上点击“下载”按钮,就将服务器的一个文件下载到本地,保存成.txt形式,该怎么保存成.txt形式,并让用户选择保存路径?
谢谢

解决方案 »

  1.   

    protected void Button1_Click( object sender, System.EventArgs e )
      {
        Response.Clear();
        Response.Buffer = true;
        Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.txt");
        Response.WriteFile("xxxxx.txt");
        Response.Flush();
        Response.End();  }
      

  2.   

    string fileName = "";//客户端保存的文件名
                string filePath = Server.MapPath("");//路径            FileInfo fileInfo = new FileInfo(filePath);
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.ContentType = "application/octet-stream";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                Response.WriteFile(fileInfo.FullName);
                Response.Flush();
                Response.End();
     //字符流方式下载文件
            protected void Button4_Click(object sender, EventArgs e)
            {
                string fileName = "";//客户端保存的文件名
                string filePath = Server.MapPath("");//路径            //以字符流的形式下载文件
                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] bytes = new byte[(int)fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment;   filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();
            }