Skip to content

Commit

Permalink
https://github.com/arivera12/BlazorDownloadFile/pull/36
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Rivera committed Apr 23, 2022
1 parent 8a3530a commit 5051945
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 46 deletions.
8 changes: 4 additions & 4 deletions BlazorDownloadFile/BlazorDownloadFile.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
<PackageProjectUrl>https://github.com/arivera12/BlazorDownloadFile</PackageProjectUrl>
<RepositoryUrl>https://github.com/arivera12/BlazorDownloadFile</RepositoryUrl>
<RepositoryType>blazor, c#</RepositoryType>
<PackageReleaseNotes>Fix of fraction portion loss for progress reporting</PackageReleaseNotes>
<PackageReleaseNotes>Changed IProgress&lt;double&gt;? progress to Func&lt;double, Task&gt; progress because IProgress is synchronous. Converting the synchronous IProgress to an asynchronous Task and awaiting a very small Task.Delay() during the progress report gives the browser enough time to process any updates for any UI refreshing.</PackageReleaseNotes>
<Authors>Anthony G. Rivera Cosme</Authors>
<Company></Company>
<Copyright>Anthony G. Rivera Cosme</Copyright>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<Version>2.3.1.1</Version>
<AssemblyVersion>2.3.1.1</AssemblyVersion>
<FileVersion>2.3.1.1</FileVersion>
<Version>2.4.0.0</Version>
<AssemblyVersion>2.4.0.0</AssemblyVersion>
<FileVersion>2.4.0.0</FileVersion>
</PropertyGroup>


Expand Down
54 changes: 27 additions & 27 deletions BlazorDownloadFile/Service/BlazorDownloadFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,91 +220,91 @@ public ValueTask<DownloadFileResult> DownloadFileFromText(string fileName, strin
return DownloadFile(fileName, plainText.ToBase64Encode(encoding, encoderShouldEmitUTF8Identifier), timeOut, contentType);
}
/// <inheritdoc/>
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, string bytesBase64, int bufferSize = 32768, string contentType = "application/octet-stream", IProgress<double>? progress = null)
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, string bytesBase64, int bufferSize = 32768, string contentType = "application/octet-stream", Func<double, Task> progress = null)
{
await ClearBuffers();
var bytesReaded = 0;
foreach (var partFile in Partition(bytesBase64, bufferSize))
{
bytesReaded += partFile.Count;
var totalProgress = (double)bytesReaded / bytesBase64.Length;
progress?.Report(totalProgress);
progress?.Invoke(totalProgress);
await JSRuntime.InvokeVoidAsync("_blazorDownloadFileBuffersPush", string.Join("", partFile));
}
return await JSRuntime.InvokeAsync<DownloadFileResult>("_blazorDowloadFileBase64StringPartitioned", fileName, contentType);
}
/// <inheritdoc/>
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, string bytesBase64, CancellationToken cancellationToken, int bufferSize = 32768, string contentType = "application/octet-stream", IProgress<double>? progress = null)
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, string bytesBase64, CancellationToken cancellationToken, int bufferSize = 32768, string contentType = "application/octet-stream", Func<double, Task> progress = null)
{
await ClearBuffers();
var bytesReaded = 0;
foreach (var partFile in Partition(bytesBase64, bufferSize))
{
bytesReaded += partFile.Count;
var totalProgress = (double)bytesReaded / bytesBase64.Length;
progress?.Report(totalProgress);
progress?.Invoke(totalProgress);
await JSRuntime.InvokeVoidAsync("_blazorDownloadFileBuffersPush", cancellationToken, string.Join("", partFile));
}
return await JSRuntime.InvokeAsync<DownloadFileResult>("_blazorDowloadFileBase64StringPartitioned", cancellationToken, fileName, contentType);
}
/// <inheritdoc/>
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, string bytesBase64, TimeSpan timeOut, int bufferSize = 32768, string contentType = "application/octet-stream", IProgress<double>? progress = null)
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, string bytesBase64, TimeSpan timeOut, int bufferSize = 32768, string contentType = "application/octet-stream", Func<double, Task> progress = null)
{
await ClearBuffers();
var bytesReaded = 0;
foreach (var partFile in Partition(bytesBase64, bufferSize))
{
bytesReaded += partFile.Count;
var totalProgress = (double)bytesReaded / bytesBase64.Length;
progress?.Report(totalProgress);
progress?.Invoke(totalProgress);
await JSRuntime.InvokeVoidAsync("_blazorDownloadFileBuffersPush", timeOut, string.Join("", partFile));
}
return await JSRuntime.InvokeAsync<DownloadFileResult>("_blazorDowloadFileBase64StringPartitioned", timeOut, fileName, contentType);
}
/// <inheritdoc/>
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, byte[] bytes, int bufferSize = 32768, string contentType = "application/octet-stream", IProgress<double>? progress = null)
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, byte[] bytes, int bufferSize = 32768, string contentType = "application/octet-stream", Func<double, Task> progress = null)
{
await ClearBuffers();
var bytesReaded = 0;
foreach (var partFile in Partition(bytes, bufferSize))
{
bytesReaded += partFile.Count;
var totalProgress = (double)bytesReaded / bytes.Length;
progress?.Report(totalProgress);
progress?.Invoke(totalProgress);
await JSRuntime.InvokeVoidAsync("_blazorDownloadFileBuffersPush", partFile);
}
return await JSRuntime.InvokeAsync<DownloadFileResult>("_blazorDowloadFileByteArrayPartitioned", fileName, contentType);
}
/// <inheritdoc/>
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, byte[] bytes, CancellationToken cancellationToken, int bufferSize = 32768, string contentType = "application/octet-stream", IProgress<double>? progress = null)
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, byte[] bytes, CancellationToken cancellationToken, int bufferSize = 32768, string contentType = "application/octet-stream", Func<double, Task> progress = null)
{
await ClearBuffers();
var bytesReaded = 0;
foreach (var partFile in Partition(bytes, bufferSize))
{
bytesReaded += partFile.Count;
var totalProgress = (double)bytesReaded / bytes.Length;
progress?.Report(totalProgress);
progress?.Invoke(totalProgress);
await JSRuntime.InvokeVoidAsync("_blazorDownloadFileBuffersPush", cancellationToken, partFile);
}
return await JSRuntime.InvokeAsync<DownloadFileResult>("_blazorDowloadFileByteArrayPartitioned", cancellationToken, fileName, contentType);
}
/// <inheritdoc/>
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, byte[] bytes, TimeSpan timeOut, int bufferSize = 32768, string contentType = "application/octet-stream", IProgress<double>? progress = null)
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, byte[] bytes, TimeSpan timeOut, int bufferSize = 32768, string contentType = "application/octet-stream", Func<double, Task> progress = null)
{
await ClearBuffers();
var bytesReaded = 0;
foreach (var partFile in Partition(bytes, bufferSize))
{
bytesReaded += partFile.Count;
var totalProgress = (double)bytesReaded / bytes.Length;
progress?.Report(totalProgress);
progress?.Invoke(totalProgress);
await JSRuntime.InvokeVoidAsync("_blazorDownloadFileBuffersPush", timeOut, partFile);
}
return await JSRuntime.InvokeAsync<DownloadFileResult>("_blazorDowloadFileByteArrayPartitioned", timeOut, fileName, contentType);
}
/// <inheritdoc/>
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, IEnumerable<byte> bytes, int bufferSize = 32768, string contentType = "application/octet-stream", IProgress<double>? progress = null)
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, IEnumerable<byte> bytes, int bufferSize = 32768, string contentType = "application/octet-stream", Func<double, Task> progress = null)
{
await ClearBuffers();
var bytesReaded = 0;
Expand All @@ -313,13 +313,13 @@ public async ValueTask<DownloadFileResult> DownloadFile(string fileName, IEnumer
{
bytesReaded += partFile.Count;
var totalProgress = (double)bytesReaded / bytesLength;
progress?.Report(totalProgress);
progress?.Invoke(totalProgress);
await JSRuntime.InvokeVoidAsync("_blazorDownloadFileBuffersPush", partFile);
}
return await JSRuntime.InvokeAsync<DownloadFileResult>("_blazorDowloadFileByteArrayPartitioned", fileName, contentType);
}
/// <inheritdoc/>
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, IEnumerable<byte> bytes, CancellationToken cancellationToken, int bufferSize = 32768, string contentType = "application/octet-stream", IProgress<double>? progress = null)
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, IEnumerable<byte> bytes, CancellationToken cancellationToken, int bufferSize = 32768, string contentType = "application/octet-stream", Func<double, Task> progress = null)
{
await ClearBuffers();
var bytesReaded = 0;
Expand All @@ -328,13 +328,13 @@ public async ValueTask<DownloadFileResult> DownloadFile(string fileName, IEnumer
{
bytesReaded += partFile.Count;
var totalProgress = (double)bytesReaded / bytesLength;
progress?.Report(totalProgress);
progress?.Invoke(totalProgress);
await JSRuntime.InvokeVoidAsync("_blazorDownloadFileBuffersPush", cancellationToken, partFile);
}
return await JSRuntime.InvokeAsync<DownloadFileResult>("_blazorDowloadFileByteArrayPartitioned", cancellationToken, fileName, contentType);
}
/// <inheritdoc/>
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, IEnumerable<byte> bytes, TimeSpan timeOut, int bufferSize = 32768, string contentType = "application/octet-stream", IProgress<double>? progress = null)
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, IEnumerable<byte> bytes, TimeSpan timeOut, int bufferSize = 32768, string contentType = "application/octet-stream", Func<double, Task> progress = null)
{
await ClearBuffers();
var bytesReaded = 0;
Expand All @@ -343,13 +343,13 @@ public async ValueTask<DownloadFileResult> DownloadFile(string fileName, IEnumer
{
bytesReaded += partFile.Count;
var totalProgress = (double)bytesReaded / bytesLength;
progress?.Report(totalProgress);
progress?.Invoke(totalProgress);
await JSRuntime.InvokeVoidAsync("_blazorDownloadFileBuffersPush", timeOut, partFile);
}
return await JSRuntime.InvokeAsync<DownloadFileResult>("_blazorDowloadFileByteArrayPartitioned", timeOut, fileName, contentType);
}
/// <inheritdoc/>
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, Stream stream, int bufferSize = 32768, string contentType = "application/octet-stream", IProgress<double>? progress = null)
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, Stream stream, int bufferSize = 32768, string contentType = "application/octet-stream", Func<double, Task> progress = null)
{
await ClearBuffers();
var totalOfBytes = (int)stream.Length;
Expand All @@ -362,13 +362,13 @@ public async ValueTask<DownloadFileResult> DownloadFile(string fileName, Stream
totalOfBytesReaded += await stream.ReadAsync(buffer, 0, currentBufferSize);
pendingBytesToRead -= totalOfBytesReaded;
var totalProgress = (double)totalOfBytesReaded / totalOfBytes;
progress?.Report(totalProgress);
progress?.Invoke(totalProgress);
await JSRuntime.InvokeVoidAsync("_blazorDownloadFileBuffersPush", buffer.Select(b => b));
} while (pendingBytesToRead > 0);
return await JSRuntime.InvokeAsync<DownloadFileResult>("_blazorDowloadFileByteArrayPartitioned", fileName, contentType);
}
/// <inheritdoc/>
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, Stream stream, CancellationToken cancellationTokenBytesRead, CancellationToken cancellationTokenJavaScriptInterop, int bufferSize = 32768, string contentType = "application/octet-stream", IProgress<double>? progress = null)
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, Stream stream, CancellationToken cancellationTokenBytesRead, CancellationToken cancellationTokenJavaScriptInterop, int bufferSize = 32768, string contentType = "application/octet-stream", Func<double, Task> progress = null)
{
await ClearBuffers();
var totalOfBytes = (int)stream.Length;
Expand All @@ -381,13 +381,13 @@ public async ValueTask<DownloadFileResult> DownloadFile(string fileName, Stream
totalOfBytesReaded += await stream.ReadAsync(buffer, 0, currentBufferSize, cancellationTokenBytesRead);
pendingBytesToRead -= totalOfBytesReaded;
var totalProgress = (double)totalOfBytesReaded / totalOfBytes;
progress?.Report(totalProgress);
progress?.Invoke(totalProgress);
await JSRuntime.InvokeVoidAsync("_blazorDownloadFileBuffersPush", cancellationTokenJavaScriptInterop, buffer.Select(b => b));
} while (pendingBytesToRead > 0);
return await JSRuntime.InvokeAsync<DownloadFileResult>("_blazorDowloadFileByteArrayPartitioned", cancellationTokenJavaScriptInterop, fileName, contentType);
}
/// <inheritdoc/>
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, Stream stream, CancellationToken cancellationTokenBytesRead, TimeSpan timeOutJavaScriptInterop, int bufferSize = 32768, string contentType = "application/octet-stream", IProgress<double>? progress = null)
public async ValueTask<DownloadFileResult> DownloadFile(string fileName, Stream stream, CancellationToken cancellationTokenBytesRead, TimeSpan timeOutJavaScriptInterop, int bufferSize = 32768, string contentType = "application/octet-stream", Func<double, Task> progress = null)
{
await ClearBuffers();
var totalOfBytes = (int)stream.Length;
Expand All @@ -400,23 +400,23 @@ public async ValueTask<DownloadFileResult> DownloadFile(string fileName, Stream
totalOfBytesReaded += await stream.ReadAsync(buffer, 0, currentBufferSize, cancellationTokenBytesRead);
pendingBytesToRead -= totalOfBytesReaded;
var totalProgress = (double)totalOfBytesReaded / totalOfBytes;
progress?.Report(totalProgress);
progress?.Invoke(totalProgress);
await JSRuntime.InvokeVoidAsync("_blazorDownloadFileBuffersPush", timeOutJavaScriptInterop, buffer.Select(b => b));
} while (pendingBytesToRead > 0);
return await JSRuntime.InvokeAsync<DownloadFileResult>("_blazorDowloadFileByteArrayPartitioned", timeOutJavaScriptInterop, fileName, contentType);
}
/// <inheritdoc/>
public ValueTask<DownloadFileResult> DownloadFileFromText(string fileName, string plainText, Encoding encoding, int bufferSize = 32768, string contentType = "text/plain", IProgress<double>? progress = null, bool encoderShouldEmitUTF8Identifier = false)
public ValueTask<DownloadFileResult> DownloadFileFromText(string fileName, string plainText, Encoding encoding, int bufferSize = 32768, string contentType = "text/plain", Func<double, Task> progress = null, bool encoderShouldEmitUTF8Identifier = false)
{
return DownloadFile(fileName, plainText.ToBase64Encode(encoding, encoderShouldEmitUTF8Identifier), bufferSize, contentType, progress);
}
/// <inheritdoc/>
public ValueTask<DownloadFileResult> DownloadFileFromText(string fileName, string plainText, Encoding encoding, CancellationToken cancellationToken, int bufferSize = 32768, string contentType = "text/plain", IProgress<double>? progress = null, bool encoderShouldEmitUTF8Identifier = false)
public ValueTask<DownloadFileResult> DownloadFileFromText(string fileName, string plainText, Encoding encoding, CancellationToken cancellationToken, int bufferSize = 32768, string contentType = "text/plain", Func<double, Task> progress = null, bool encoderShouldEmitUTF8Identifier = false)
{
return DownloadFile(fileName, plainText.ToBase64Encode(encoding, encoderShouldEmitUTF8Identifier), cancellationToken, bufferSize, contentType, progress);
}
/// <inheritdoc/>
public ValueTask<DownloadFileResult> DownloadFileFromText(string fileName, string plainText, Encoding encoding, TimeSpan timeOut, int bufferSize = 32768, string contentType = "text/plain", IProgress<double>? progress = null, bool encoderShouldEmitUTF8Identifier = false)
public ValueTask<DownloadFileResult> DownloadFileFromText(string fileName, string plainText, Encoding encoding, TimeSpan timeOut, int bufferSize = 32768, string contentType = "text/plain", Func<double, Task> progress = null, bool encoderShouldEmitUTF8Identifier = false)
{
return DownloadFile(fileName, plainText.ToBase64Encode(encoding, encoderShouldEmitUTF8Identifier), timeOut, bufferSize, contentType, progress);
}
Expand Down
Loading

0 comments on commit 5051945

Please sign in to comment.