using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Web; using System.Web.Mvc; namespace WebApi_QQJF.Controllers { public class HomeController : Controller { public ActionResult Index() { ViewBag.Title = "Home Page"; return View(); } /// /// 文件下载 /// /// /// [HttpGet] public void GetDownLoad(string name) { //StringBuilder strSql = new StringBuilder(); //string LastName = System.IO.Path.GetExtension(name); //string customFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + LastName; //string filePath = System.Web.HttpContext.Current.Request.PhysicalPath+"..\\..\\..\\" + name; //FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); //HttpResponseMessage response = new HttpResponseMessage(); //response.Content = new StreamContent(fileStream); //response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); //response.Content.Headers.ContentDisposition.FileName = customFileName; //response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); // 这句话要告诉浏览器要下载文件 //response.Content.Headers.ContentLength = new FileInfo(filePath).Length; //return response; string FilePath = System.Web.HttpContext.Current.Request.PhysicalPath + "..\\..\\..\\" + name; //以字符流的形式下载文件 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;charset=gb2321"; //通知浏览器下载文件而不是打开;对中文名称进行编码 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("111.apk", System.Text.Encoding.UTF8)); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); } } }