From bd90fb52f87afdf55a10926aafa612686f4e86df Mon Sep 17 00:00:00 2001 From: zhangpeihang <948869991@qq.com> Date: Tue, 15 Feb 2022 13:12:19 +0800 Subject: [PATCH 1/7] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E7=89=A9=E7=90=86=E8=B7=AF=E5=BE=84=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=92=8C=E6=96=87=E4=BB=B6=E5=A4=B9=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9=E4=B8=8B=E8=BD=BD=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Download/DownloadService.cs | 74 ++++++++++++++++--- 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/src/BootstrapBlazor/Components/Download/DownloadService.cs b/src/BootstrapBlazor/Components/Download/DownloadService.cs index d5e2bf4a9..05a686b41 100644 --- a/src/BootstrapBlazor/Components/Download/DownloadService.cs +++ b/src/BootstrapBlazor/Components/Download/DownloadService.cs @@ -3,6 +3,7 @@ // Website: https://www.blazor.zone or https://argozhang.github.io/ using Microsoft.AspNetCore.Components; +using System.IO.Compression; namespace BootstrapBlazor.Components; @@ -62,50 +63,103 @@ public class DownloadService /// /// 下载文件方法 /// - /// 文件名 + /// 文件名 /// 文件流 /// /// - public async Task DownloadAsync(string fileName, Stream stream, string mime = "application/octet-stream") + public async Task DownloadAsync(string downloadFileName, Stream stream, string mime = "application/octet-stream") { var bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); stream.Seek(0, SeekOrigin.Begin); - await DownloadAsync(new DownloadOption() { FileName = fileName, FileContent = bytes, Mime = mime }); + await DownloadAsync(new DownloadOption() { FileName = downloadFileName, FileContent = bytes, Mime = mime }); } /// /// 下载文件方法 /// - /// 文件名 + /// 文件名 + /// 文件物理路径 + /// + /// + public async Task DownloadAsync(string downloadFileName, string physicalPath, string mime = "application/octet-stream") + { + if (File.Exists(physicalPath)) + { + using var stream = new FileStream(physicalPath, 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 }); + } + else + { + + throw new FileNotFoundException($"Can't be not found {physicalPath}"); + } + } + + /// + /// 下载文件方法 + /// + /// 文件名 /// 文件内容 byte[] 数组 /// /// - public Task DownloadAsync(string fileName, byte[] fileContent, string mime = "application/octet-stream") => DownloadAsync(new DownloadOption() { FileName = fileName, FileContent = fileContent, Mime = mime }); + public Task DownloadAsync(string downloadFileName, byte[] fileContent, string mime = "application/octet-stream") => DownloadAsync(new DownloadOption() { FileName = downloadFileName, FileContent = fileContent, Mime = mime }); /// /// 获取文件连接方法 /// - /// 文件名 + /// 文件名 /// 文件流 /// /// - public async Task CreateUrlAsync(string fileName, Stream stream, string mime = "application/octet-stream") + public async Task CreateUrlAsync(string downloadFileName, Stream stream, string mime = "application/octet-stream") { var bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); stream.Seek(0, SeekOrigin.Begin); - return await CreateUrlAsync(new DownloadOption() { FileName = fileName, FileContent = bytes, Mime = mime }); + return await CreateUrlAsync(new DownloadOption() { FileName = downloadFileName, FileContent = bytes, Mime = mime }); } /// /// 获取文件连接方法 /// - /// 文件名 + /// 文件名 /// 文件内容 byte[] 数组 /// /// - public Task CreateUrlAsync(string fileName, byte[] fileContent, string mime = "application/octet-stream") => CreateUrlAsync(new DownloadOption() { FileName = fileName, FileContent = fileContent, Mime = mime }); + public Task CreateUrlAsync(string downloadFileName, byte[] fileContent, string mime = "application/octet-stream") => CreateUrlAsync(new DownloadOption() { FileName = downloadFileName, FileContent = fileContent, Mime = mime }); + + /// + /// 下载文件夹方法 + /// + /// 文件名 + /// 文件夹路径 + /// + /// + public async Task DownloadDirectoryAsync(string downloadFileName, string directory, string mime = "application/octet-stream") + { + if (Directory.Exists(directory)) + { + // 打包文件 + var directoryName = directory.TrimEnd('\\', '\r', '\n'); + var destZipFile = $"{directoryName}.zip"; + ZipFile.CreateFromDirectory(directory, destZipFile); + + using var stream = new FileStream(destZipFile, System.IO.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 }); + } + else + { + + throw new DirectoryNotFoundException($"Can't be not found {directory}"); + } + } /// /// 下载文件方法 -- Gitee From 5e2e47ffbb0a8243a0237a438f4b9e274f95fa47 Mon Sep 17 00:00:00 2001 From: zhangpeihang <948869991@qq.com> Date: Tue, 15 Feb 2022 13:24:00 +0800 Subject: [PATCH 2/7] =?UTF-8?q?doc:=20=E8=A1=A5=E5=85=85=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Samples/Downloads.razor | 8 +++++ .../Samples/Downloads.razor.cs | 29 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/BootstrapBlazor.Shared/Samples/Downloads.razor b/src/BootstrapBlazor.Shared/Samples/Downloads.razor index 54594cfb2..7641532b9 100644 --- a/src/BootstrapBlazor.Shared/Samples/Downloads.razor +++ b/src/BootstrapBlazor.Shared/Samples/Downloads.razor @@ -46,4 +46,12 @@ + + + + + + + + diff --git a/src/BootstrapBlazor.Shared/Samples/Downloads.razor.cs b/src/BootstrapBlazor.Shared/Samples/Downloads.razor.cs index c176458eb..ca99a012a 100644 --- a/src/BootstrapBlazor.Shared/Samples/Downloads.razor.cs +++ b/src/BootstrapBlazor.Shared/Samples/Downloads.razor.cs @@ -85,4 +85,33 @@ public partial class Downloads return ms; } }); + + private Task DownloadPhysicalFileAsync() => Task.Run(async () => + { + try + { + var filePath = Path.Combine(SiteOptions.Value.WebRootPath, "favicon.png"); + await downloadService.DownloadAsync("favicon.png", filePath); + } + catch (FileNotFoundException msg) + { + + await ToastService.Error("下载", msg.Message); + } + + }); + + private Task DownloadDirectoryAsync() => Task.Run(async () => + { + try + { + await downloadService.DownloadDirectoryAsync("test.zip", SiteOptions.Value.WebRootPath); + } + catch (FileNotFoundException msg) + { + + await ToastService.Error("下载", msg.Message); + } + + }); } -- Gitee From d398679bd8a6cf1ccb4b5bdd2eba56cba82c416a Mon Sep 17 00:00:00 2001 From: zhangpeihang <948869991@qq.com> Date: Tue, 15 Feb 2022 13:27:18 +0800 Subject: [PATCH 3/7] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E7=AD=BE=E5=90=8D=E4=BB=A5=E5=8F=8A=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Download/DownloadService.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/BootstrapBlazor/Components/Download/DownloadService.cs b/src/BootstrapBlazor/Components/Download/DownloadService.cs index 05a686b41..6e8bebd9d 100644 --- a/src/BootstrapBlazor/Components/Download/DownloadService.cs +++ b/src/BootstrapBlazor/Components/Download/DownloadService.cs @@ -95,7 +95,7 @@ public class DownloadService else { - throw new FileNotFoundException($"Can't be not found {physicalPath}"); + throw new FileNotFoundException($"Couldn't be not found {physicalPath}"); } } @@ -136,19 +136,19 @@ public class DownloadService /// 下载文件夹方法 /// /// 文件名 - /// 文件夹路径 + /// 文件夹路径 /// /// - public async Task DownloadDirectoryAsync(string downloadFileName, string directory, string mime = "application/octet-stream") + public async Task DownloadFolderAsync(string downloadFileName, string folder, string mime = "application/octet-stream") { - if (Directory.Exists(directory)) + if (Directory.Exists(folder)) { // 打包文件 - var directoryName = directory.TrimEnd('\\', '\r', '\n'); + var directoryName = folder.TrimEnd('\\', '\r', '\n'); var destZipFile = $"{directoryName}.zip"; - ZipFile.CreateFromDirectory(directory, destZipFile); + ZipFile.CreateFromDirectory(folder, destZipFile); - using var stream = new FileStream(destZipFile, System.IO.FileMode.Open); + 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); @@ -157,7 +157,7 @@ public class DownloadService else { - throw new DirectoryNotFoundException($"Can't be not found {directory}"); + throw new DirectoryNotFoundException($"Couldn't be not found {folder}"); } } -- Gitee From 3a78eb9358b2b86b03c5967ff1197f0ac74e85e3 Mon Sep 17 00:00:00 2001 From: zhangpeihang <948869991@qq.com> Date: Tue, 15 Feb 2022 13:28:18 +0800 Subject: [PATCH 4/7] =?UTF-8?q?doc:=20=E4=BF=AE=E6=94=B9=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit doc: 修改文档 --- .../Samples/Downloads.razor | 16 ++++++++-------- .../Samples/Downloads.razor.cs | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/BootstrapBlazor.Shared/Samples/Downloads.razor b/src/BootstrapBlazor.Shared/Samples/Downloads.razor index 7641532b9..0347129e6 100644 --- a/src/BootstrapBlazor.Shared/Samples/Downloads.razor +++ b/src/BootstrapBlazor.Shared/Samples/Downloads.razor @@ -36,6 +36,14 @@ } + + + + + + + +

按钮设置 IsAsync 值为 true 进行异步下载操作

@@ -46,12 +54,4 @@
- - - - - - - - diff --git a/src/BootstrapBlazor.Shared/Samples/Downloads.razor.cs b/src/BootstrapBlazor.Shared/Samples/Downloads.razor.cs index ca99a012a..80796a7c1 100644 --- a/src/BootstrapBlazor.Shared/Samples/Downloads.razor.cs +++ b/src/BootstrapBlazor.Shared/Samples/Downloads.razor.cs @@ -101,11 +101,11 @@ public partial class Downloads }); - private Task DownloadDirectoryAsync() => Task.Run(async () => + private Task DownloadFolderAsync() => Task.Run(async () => { try { - await downloadService.DownloadDirectoryAsync("test.zip", SiteOptions.Value.WebRootPath); + await downloadService.DownloadFolderAsync("test.zip", SiteOptions.Value.WebRootPath); } catch (FileNotFoundException msg) { -- Gitee From 8d4aee692f754f51a1e2772a0dbfe70f3ee64d99 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Tue, 15 Feb 2022 23:29:26 +0800 Subject: [PATCH 5/7] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Download/DownloadService.cs | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/BootstrapBlazor/Components/Download/DownloadService.cs b/src/BootstrapBlazor/Components/Download/DownloadService.cs index 6e8bebd9d..6d06eb495 100644 --- a/src/BootstrapBlazor/Components/Download/DownloadService.cs +++ b/src/BootstrapBlazor/Components/Download/DownloadService.cs @@ -79,24 +79,21 @@ public class DownloadService /// 下载文件方法 /// /// 文件名 - /// 文件物理路径 + /// 文件物理路径 /// /// - public async Task DownloadAsync(string downloadFileName, string physicalPath, string mime = "application/octet-stream") + public async Task DownloadAsync(string downloadFileName, string physicalFilePath, string mime = "application/octet-stream") { - if (File.Exists(physicalPath)) + if (!File.Exists(physicalFilePath)) { - using var stream = new FileStream(physicalPath, 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 }); + throw new FileNotFoundException($"Couldn't be not found {physicalFilePath}", physicalFilePath); } - else - { - throw new FileNotFoundException($"Couldn't be not found {physicalPath}"); - } + using var stream = new FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); + 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 }); } /// -- Gitee From c5eed1e168d20d79d156cad664d48759978cebb5 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Tue, 15 Feb 2022 23:29:52 +0800 Subject: [PATCH 6/7] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BC=98=E5=85=88=E5=88=A4=E6=96=AD=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9=E4=B8=8D=E5=AD=98=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Download/DownloadService.cs | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/BootstrapBlazor/Components/Download/DownloadService.cs b/src/BootstrapBlazor/Components/Download/DownloadService.cs index 6d06eb495..cc213564b 100644 --- a/src/BootstrapBlazor/Components/Download/DownloadService.cs +++ b/src/BootstrapBlazor/Components/Download/DownloadService.cs @@ -138,24 +138,21 @@ public class DownloadService /// public async Task DownloadFolderAsync(string downloadFileName, string folder, string mime = "application/octet-stream") { - if (Directory.Exists(folder)) + if (!Directory.Exists(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 }); - } - else - { - 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 }); } /// -- Gitee From f749d60ccbb566ac67278984e1e7d78576ea34b2 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Tue, 15 Feb 2022 23:30:05 +0800 Subject: [PATCH 7/7] =?UTF-8?q?docs:=20=E6=9B=B4=E6=96=B0=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Samples/Downloads.razor | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/BootstrapBlazor.Shared/Samples/Downloads.razor b/src/BootstrapBlazor.Shared/Samples/Downloads.razor index 0347129e6..3fc9bf534 100644 --- a/src/BootstrapBlazor.Shared/Samples/Downloads.razor +++ b/src/BootstrapBlazor.Shared/Samples/Downloads.razor @@ -4,22 +4,21 @@

用于直接下载物理文件

- -

- 特别注意: -

- Blazorjs 的交互使用了 json,在 ssr 模式中,json 最大传输大小是 125M,这是 asp.net core 的限制。并且由于 jsonjsblob 非常非常慢,所以大文件请谨慎,建议只在类似页面文件导出、报表图片下载等页面自行完成的内容中使用。 - 其他例如服务器文件下载等依旧使用 Controller 来完成。如果下载大文件请自行仔细测试 -
-

-
- 示例: - +

+ 特别注意: +

+ Blazorjs 的交互使用了 json,在 ssr 模式中,json 最大传输大小是 125M,这是 asp.net core 的限制。并且由于 jsonjsblob 非常非常慢,所以大文件请谨慎,建议只在类似页面文件导出、报表图片下载等页面自行完成的内容中使用。 + 其他例如服务器文件下载等依旧使用 Controller 来完成。如果下载大文件请自行仔细测试
-
Razor 代码
-
<Button OnClick="DownloadFile">点我下载文件</Button>
-
C# 代码
-
private async Task DownloadFileAsync()
+

+
+ 示例: + +
+
Razor 代码
+
<Button OnClick="DownloadFile">点我下载文件</Button>
+
C# 代码
+
private async Task DownloadFileAsync()
 {
     var content = await GenerateFileAsync();
     await downloadService.DownloadAsync("测试文件", content);
@@ -34,13 +33,12 @@
         return ms.ToArray();
     }
 }
- - + - + @@ -53,5 +51,3 @@

由于测试使用了wwwroot下的文件,没有代码生成,wasm无法访问wwwroot文件夹,故此测试只有ssr模式可用。wasm请自行测试。

- - -- Gitee