MVC 下載檔案

xuxubaby發表於2016-09-05

.net mvc 檔案下載


 /// <summary>
        /// 下載檔案
        /// </summary>
        /// <returns></returns>
        public ActionResult DownFile()
        {
            string name = RequestHelper.GetString("",Request["name"]);
            string fileName = "xxxx.exe";//客戶端儲存的檔名  
            string filePath = Server.MapPath("/DBBack/" + name);//路徑  

            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();
        }



相關文章