Skip to content

Commit 342ff51

Browse files
authored
pef(Upload): add bufferSize parameter for improve save file performance (#6314)
* refactor: 精简代码 * feat: 增加 BufferSize 参数 * doc: 更新示例文档 * chore: bump version 9.8.0-beta06
1 parent 5779d48 commit 342ff51

File tree

4 files changed

+7
-6
lines changed

4 files changed

+7
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private async Task SaveToFile(UploadFile file)
6161
_token ??= new CancellationTokenSource();
6262
try
6363
{
64-
var ret = await file.SaveToFileAsync(fileName, MaxFileLength, _token.Token);
64+
var ret = await file.SaveToFileAsync(fileName, MaxFileLength, token: _token.Token);
6565

6666
if (ret)
6767
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private async Task SaveToFile(UploadFile file)
7878
_token ??= new CancellationTokenSource();
7979
try
8080
{
81-
var ret = await file.SaveToFileAsync(fileName, MaxFileLength, _token.Token);
81+
var ret = await file.SaveToFileAsync(fileName, MaxFileLength, token: _token.Token);
8282

8383
if (ret)
8484
{

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.8.0-beta05</Version>
4+
<Version>9.8.0-beta06</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Extensions/UploadFileExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ public static async Task RequestBase64ImageFileAsync(this UploadFile upload, str
5757
/// <param name="upload"></param>
5858
/// <param name="fileName"></param>
5959
/// <param name="maxAllowedSize"></param>
60+
/// <param name="bufferSize"></param>
6061
/// <param name="token"></param>
6162
/// <returns></returns>
6263
[ExcludeFromCodeCoverage]
63-
public static async Task<bool> SaveToFileAsync(this UploadFile upload, string fileName, long maxAllowedSize = 512000, CancellationToken token = default)
64+
public static async Task<bool> SaveToFileAsync(this UploadFile upload, string fileName, long maxAllowedSize = 512000, int bufferSize = 64 * 1024, CancellationToken token = default)
6465
{
6566
var ret = false;
6667
if (upload.File != null)
@@ -92,15 +93,15 @@ public static async Task<bool> SaveToFileAsync(this UploadFile upload, string fi
9293
{
9394
// 打开文件流
9495
var stream = upload.File.OpenReadStream(maxAllowedSize, token);
95-
var buffer = new byte[4 * 1096];
96+
var buffer = new byte[bufferSize];
9697
int bytesRead = 0;
9798
double totalRead = 0;
9899

99100
// 开始读取文件
100101
while ((bytesRead = await stream.ReadAsync(buffer, token)) > 0)
101102
{
102103
totalRead += bytesRead;
103-
await uploadFile.WriteAsync(buffer.AsMemory(0, bytesRead), token);
104+
await uploadFile.WriteAsync(buffer, 0, bytesRead, token);
104105

105106
if (upload.UpdateCallback != null)
106107
{

0 commit comments

Comments
 (0)