解决方案 »

  1.   

    这个太复杂了点,有没有更简单的?直接给我后台控制器和前台View的代码就好
      

  2.   

    你在webform中怎么做的,MVC里也一样
      

  3.   

     public void XiaZai(object sender, EventArgs e)
            {
                string fileName = "QQ五笔.exe";//客户端保存的文件名 
                string filePath = Server.MapPath("../../DownLoad/QQWubi_Setup.exe");//路径             FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] bytes = new byte[(int)fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                Response.Charset = "UTF-8";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                Response.ContentType = "application/octet-stream";
                
                Response.AddHeader("Content-Disposition", "attachment; filename="+ fileName);
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();
            }这是我在控制器里的Action,可以读到那个文件,但就是不弹出下载框,运行完就没反应了。我哪里写错了?
      

  4.   

    public ActionResult File(){
    string fileName = "QQ五笔.exe";//客户端保存的文件名  
      string filePath = Server.MapPath("../../DownLoad/QQWubi_Setup.exe");//路径    FileStream fs = new FileStream(filePath, FileMode.Open);
      byte[] bytes = new byte[(int)fs.Length];
      fs.Read(bytes, 0, bytes.Length);
      fs.Close();
      Response.Charset = "UTF-8";
      Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
      Response.ContentType = "application/octet-stream";
        
      Response.AddHeader("Content-Disposition", "attachment; filename="+ fileName);
      Response.BinaryWrite(bytes);
      Response.Flush();
      Response.End();
    return new EmptyResult();
    }
      

  5.   

    为啥我用了以后没有效果呢
    var newPath = this.Server.MapPath(filePath);
                FileStream fs = new FileStream(newPath, FileMode.Open);
                byte[] bytes = new byte[(int)fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                Response.Charset = "UTF-8";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                Response.ContentType = "application/octet-stream";            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();
                return new EmptyResult();
      

  6.   


    public ActionResult DownLoad()
            {
                //插件名称
                string fileName = "虎视视频控件_Setup(v2.6.0.0).exe";
                //文件路径
                string filePath = Server.MapPath("../Video/虎视视频控件_Setup(v2.6.0.0).exe");
                FileStream fs = new FileStream(filePath,FileMode.Open);
                byte[] bytes = new byte[(int)fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                Response.Charset = "UTF-8";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                Response.ContentType = "application/octet-stream";
    //解决文件名乱码问题            
    Response.AddHeader("Content-Disposition","attachment;filename="+Server.UrlEncode(fileName));
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();
                return new EmptyResult();
            }
      

  7.   

    搞的这么麻烦,MVC很简单的啊。
    [HttpGet]
    public FilePathResult GetFile(){
      return File();
    }