Skip to content

Commit b84dd4c

Browse files
authored
doc(Uploader): add UseUploaderStaticFiles extension method (#6382)
* doc: 更新资源文件 * doc(Uploader): add UseUploaderStaticFiles extension method
1 parent b442af2 commit b84dd4c

File tree

5 files changed

+49
-12
lines changed

5 files changed

+49
-12
lines changed

src/BootstrapBlazor.Server/Components/Samples/UploadCards.razor.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

6-
using Newtonsoft.Json.Linq;
7-
86
namespace BootstrapBlazor.Server.Components.Samples;
97

108
/// <summary>
@@ -50,7 +48,7 @@ private async Task OnCardUpload(UploadFile file)
5048
// 服务器端验证当文件大于 5MB 时提示文件太大信息
5149
if (file.Size > MaxFileLength)
5250
{
53-
await ToastService.Information(Localizer["UploadsFileMsg"], Localizer["UploadsFileError"]);
51+
await ToastService.Information(Localizer["UploadsFileTitle"], Localizer["UploadsFileError"]);
5452
file.Code = 1;
5553
file.Error = Localizer["UploadsFileError"];
5654
}
@@ -59,7 +57,7 @@ private async Task OnCardUpload(UploadFile file)
5957
// 模拟保存成功
6058
await Task.Delay(100);
6159
await SaveToFile(file);
62-
await ToastService.Success(Localizer["UploadsFileMsg"], $"{file.File!.Name} {Localizer["UploadsSuccess"]}");
60+
await ToastService.Success(Localizer["UploadsFileTitle"], $"{file.File!.Name} {Localizer["UploadsSuccess"]}");
6361
}
6462
}
6563
}
@@ -87,10 +85,10 @@ private async Task SaveToFile(UploadFile file)
8785
}
8886
else
8987
{
90-
var errorMessage = $"{Localizer["UploadsSaveFileError"]} {file.OriginFileName}";
88+
var errorMessage = Localizer["UploadsSaveFileError"];
9189
file.Code = 1;
9290
file.Error = errorMessage;
93-
await ToastService.Error(Localizer["UploadFile"], errorMessage);
91+
await ToastService.Error(Localizer["UploadsFileTitle"], errorMessage);
9492
}
9593
}
9694
catch (OperationCanceledException)
@@ -102,7 +100,7 @@ private async Task SaveToFile(UploadFile file)
102100
{
103101
file.Code = 1;
104102
file.Error = Localizer["UploadsWasmError"];
105-
await ToastService.Information(Localizer["UploadsSaveFile"], Localizer["UploadsSaveFileMsg"]);
103+
await ToastService.Error(Localizer["UploadsFileTitle"], Localizer["UploadsWasmError"]);
106104
}
107105
}
108106

@@ -111,9 +109,12 @@ private async Task SaveToFile(UploadFile file)
111109
/// </summary>
112110
public void Dispose()
113111
{
114-
_token?.Cancel();
115-
_token?.Dispose();
116-
_token = null;
112+
if (_token != null)
113+
{
114+
_token.Cancel();
115+
_token.Dispose();
116+
_token = null;
117+
}
117118
GC.SuppressFinalize(this);
118119
}
119120
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
using Microsoft.Extensions.FileProviders;
7+
8+
namespace Microsoft.AspNetCore.Builder;
9+
10+
static class WebApplicationExtensions
11+
{
12+
public static void UseUploaderStaticFiles(this WebApplication app)
13+
{
14+
var uploader = Path.Combine(app.Environment.WebRootPath, "images", "uploader");
15+
Directory.CreateDirectory(uploader);
16+
17+
app.UseStaticFiles(new StaticFileOptions
18+
{
19+
FileProvider = new PhysicalFileProvider(uploader),
20+
RequestPath = "/images/uploader"
21+
});
22+
}
23+
}

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3395,7 +3395,12 @@
33953395
"UploadFileIconTemplateTitle": "Custom file icon",
33963396
"UploadFileIconTemplateIntro": "By setting the <code>IconTemplate</code> parameter and using the <code>FileIcon</code> component, you can further customize the file icon <a href=\"/file-icon\">[FileIcon example]</a>",
33973397
"UploadBase64Title": "Base64 format",
3398-
"UploadBase64Intro": "By setting the <code>PrevUrl</code> parameter value of the <code>UploadFile</code> instance, use the image content string in the <code>data:image/xxx;base64,XXXXX</code> format as the preview file path"
3398+
"UploadBase64Intro": "By setting the <code>PrevUrl</code> parameter value of the <code>UploadFile</code> instance, use the image content string in the <code>data:image/xxx;base64,XXXXX</code> format as the preview file path",
3399+
"UploadsFileTitle": "Upload",
3400+
"UploadsFileError": "The file is larger than 5M. Please reselect the file to upload.",
3401+
"UploadsSuccess": "File saved successfully",
3402+
"UploadsSaveFileError": "File save failed",
3403+
"UploadsWasmError": "In wasm mode, please call the api to save"
33993404
},
34003405
"BootstrapBlazor.Server.Components.Samples.UploadDrops": {
34013406
"UploadsTitle": "DropUpload",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3395,7 +3395,12 @@
33953395
"UploadFileIconTemplateTitle": "自定义文件图标",
33963396
"UploadFileIconTemplateIntro": "通过设置 <code>IconTemplate</code> 参数,使用 <code>FileIcon</code> 组件可以对文件图标进行进一步自定义 <a href=\"/file-icon\">[FileIcon 示例]</a>",
33973397
"UploadBase64Title": "Base64 格式文件",
3398-
"UploadBase64Intro": "通过设置 <code>UploadFile</code> 实例的 <code>PrevUrl</code> 参数值使用 <code>data:image/xxx;base64,XXXXX</code> 格式图片内容字符串作为预览文件路径"
3398+
"UploadBase64Intro": "通过设置 <code>UploadFile</code> 实例的 <code>PrevUrl</code> 参数值使用 <code>data:image/xxx;base64,XXXXX</code> 格式图片内容字符串作为预览文件路径",
3399+
"UploadsFileTitle": "文件上传",
3400+
"UploadsFileError": "文件大于 5M 请重新选择文件上传",
3401+
"UploadsSuccess": "文件保存成功",
3402+
"UploadsSaveFileError": "文件保存失败",
3403+
"UploadsWasmError": "wasm 模式请调用 api 进行保存"
33993404
},
34003405
"BootstrapBlazor.Server.Components.Samples.UploadDrops": {
34013406
"UploadsTitle": "DropUpload 拖拽上传组件",

src/BootstrapBlazor.Server/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
app.UseExceptionHandler("/Error", createScopeForErrors: true);
3636
}
3737

38+
// 增加上传目录静态资源文件
39+
app.UseUploaderStaticFiles();
40+
3841
app.UseAntiforgery();
3942
app.UseBootstrapBlazor();
4043

0 commit comments

Comments
 (0)