如何使用

  1. 通过MasterChief.DotNet.Core.Config构建下载配置文件

    <?xml version="1.0" encoding="utf-16"?>
    <DownloadConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                    FileNameEncryptorIv="0102030405060708090a0a0c0d010208"
                    FileNameEncryptorKey="DotnetDownloadConfig"
                    LimitDownloadSpeedKb="1024"
                    DownLoadMainDirectory="D:\OneDrive\软件\工具\">
    </DownloadConfig>
  2. 在WebForm下新建一般处理程序,并实现DownloadHandler抽象类

    /// <summary>
    ///     FileDownloadHandler 的摘要说明
    /// </summary>
    public class FileDownloadHandler : DownloadHandler, IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            var fileName = context.Request["fileName"];
            StartDownloading(context, fileName);
        }
    
        public bool IsReusable => false;
    
        public override void OnDownloadFailed(HttpContext context, string fileName, string filePath, string ex)
        {
            context.Response.Write(ex);
        }
    
        public override void OnDownloadSucceed(HttpContext context, string fileName, string filePath)
        {
            var result = $"文件[{fileName}]下载成功,映射路径:{filePath}";
            context.Response.Write(result);
        }
    }
  3. 在WebForm下载文件加密显示

    protected void Page_Load(object sender, EventArgs e)
    {
        string url = DownloadFileContext.Instance.EncryptFileName("typora-setup-x64.exe");
        link.NavigateUrl = "~/FileDownloadHandler.ashx?fileName=" + url;
    }
  4. 运行效果

    [开源]快速构建文件下载,支持文件加密,自定义限速-LMLPHP

04-06 01:25