diff --git a/src/BootstrapBlazor/Components/Download/DownloadService.cs b/src/BootstrapBlazor/Components/Download/DownloadService.cs index 9fb4e7eecb5691d4809553451d0de9649b2a47a2..cc213564bf11c5bbbd511bd9aa4494e594d48729 100644 --- a/src/BootstrapBlazor/Components/Download/DownloadService.cs +++ b/src/BootstrapBlazor/Components/Download/DownloadService.cs @@ -129,6 +129,32 @@ public class DownloadService /// public Task CreateUrlAsync(string downloadFileName, byte[] fileContent, string mime = "application/octet-stream") => CreateUrlAsync(new DownloadOption() { FileName = downloadFileName, FileContent = fileContent, Mime = mime }); + /// + /// 下载文件夹方法 + /// + /// 文件名 + /// 文件夹路径 + /// + /// + public async Task DownloadFolderAsync(string downloadFileName, string folder, string mime = "application/octet-stream") + { + if (!Directory.Exists(folder)) + { + throw new DirectoryNotFoundException($"Couldn't be not found {folder}"); + } + + // 打包文件 + var directoryName = folder.TrimEnd('\\', '\r', '\n'); + var destZipFile = $"{directoryName}.zip"; + ZipFile.CreateFromDirectory(folder, destZipFile); + + using var stream = new FileStream(destZipFile, FileMode.Open); + var bytes = new byte[stream.Length]; + stream.Read(bytes, 0, bytes.Length); + stream.Seek(0, SeekOrigin.Begin); + await DownloadAsync(new DownloadOption() { FileName = downloadFileName, FileContent = bytes, Mime = mime }); + } + /// /// 下载文件方法 ///