mvc 大文件下载

原帖地址:https://www.cnblogs.com/wind-ye/articles/5171578.html

文件下载类:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace pxcom
{
    /// <summary>
    /// 该类继承了ActionResult,通过重写ExecuteResult方法,进行文件的下载
    /// </summary>
    public class FileResult : ActionResult
    {
        private readonly string _filePath;//文件路径
        private readonly string _fileName;//文件名称
        public FileResult(string filePath, string fileName)
        {
            _filePath = filePath;
            _fileName = fileName;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            string fileName = _fileName;
            HttpResponseBase response = context.HttpContext.Response;
            if (File.Exists(_filePath))
            {
                FileStream fs = null;
                byte[] fileBuffer = new byte[1024];//每次读取1024字节大小的数据
                try
                {
                    using (fs = File.OpenRead(_filePath))
                    {
                        long totalLength = fs.Length;
                        response.ContentType = "application/octet-stream";
                        response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
                        while (totalLength > 0 && response.IsClientConnected)//持续传输文件
                        {
                            int length = fs.Read(fileBuffer, 0, fileBuffer.Length);//每次读取1024个字节长度的内容
                            fs.Flush();
                            response.OutputStream.Write(fileBuffer, 0, length);//写入到响应的输出流
                            response.Flush();//刷新响应
                            totalLength = totalLength - length;
                        }
                        response.Close();//文件传输完毕,关闭相应流
                    }
                }
                catch (Exception ex)
                {
                    response.Write(ex.Message);
                }
                finally
                {
                    if (fs != null)
                        fs.Close();//最后记得关闭文件流
                }
            }
        }
    }
}

控制器:

   public ActionResult GetFile(string url)
        {
            string path = pxcom.Util.GetKejianPath() + url;
            var filename = Path.GetFileName(path);
            return new pxcom.FileResult(path, filename);
        }

地址栏访问:http://localhost:30324/test/getfile?url=/131575214624030721.zip

webclient下载:

public static void Down(string url,string saveFileName)
        {
            string svr = System.Configuration.ConfigurationManager.AppSettings["svraddress"];
            url = svr + "/" + url;

            new WebClient().DownloadFile(url, saveFileName);
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值