liuying
2025-08-08 a92072dcf0336eaef8f4430dad90ceb5390b28f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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();
        }
 
        /// <summary>  
        /// 文件下载  
        /// </summary>  
        /// <param name="filePath "></param>  
        /// <returns></returns>  
        [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();
        }
 
 
        
    }
}