C# 为什么在一般处理程序无法实现文件下载我在一般处理程序调用如下方法//文件下载到客户端
    public void FileDownload(string FileName)
    {
        String FullFileName = FileName;// System.Web.HttpContext.Current.Server.MapPath(FileName);
        FileInfo DownloadFile = new FileInfo(FullFileName);
        System.Web.HttpContext.Current.Response.Clear();
        System.Web.HttpContext.Current.Response.ClearHeaders();
        System.Web.HttpContext.Current.Response.Buffer = false;
        System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
        System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=module.zip");
        System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
        System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
        System.Web.HttpContext.Current.Response.Flush();
        System.Web.HttpContext.Current.Response.End();
    }执行页面会返回 乱码

解决方案 »

  1.   

                string filePath = “”;
                FileInfo Fi = new FileInfo(filePath);
                if (Fi.Exists)
                {
                    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("1.txt",System.Text.Encoding.UTF8));
                    Response.BinaryWrite(bytes);
                    Response.Flush();
                    Response.End();
                }
      

  2.   

    string path = Server.MapPath("~/") + "";
    Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.GetEncoding("utf-8")));
    Response.ContentType = "application/octet-stream";
    Response.WriteFile("" + path + "");
    Response.End();  
     
    也可使用
      FileStream fs = new FileStream(filePath, FileMode.Open);
      byte[] bytes = new byte[(int)fs.Length];
      fs.Read(bytes, 0, bytes.Length);
      fs.Close();
      

  3.   

    如果不行,就用ftpwebrequest下载数据
      

  4.   


    晕,你直接粘贴了?没有修改下?那只是个原理。你必须要加上context才行,而且你不能使用System.Web.HttpContext.Current在加上个参数public void FileDownload(string FileName,HttpContext context)