Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions 5.0/BlazorSample_Server/LazyBrowserFileStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Forms;

namespace BlazorSample;

internal sealed class LazyBrowserFileStream : Stream
{
private readonly IBrowserFile file;
private readonly int maxAllowedSize;
private Stream? underlyingStream;
private bool isDisposed;

public override bool CanRead => true;

public override bool CanSeek => false;

public override bool CanWrite => false;

public override long Length => file.Size;

public override long Position
{
get => underlyingStream?.Position ?? 0;
set => throw new NotSupportedException();
}

public LazyBrowserFileStream(IBrowserFile file, int maxAllowedSize)
{
this.file = file;
this.maxAllowedSize = maxAllowedSize;
}

public override void Flush()
{
underlyingStream?.Flush();
}

public override Task<int> ReadAsync(byte[] buffer, int offset, int count,
CancellationToken cancellationToken)
{
EnsureStreamIsOpen();

return underlyingStream.ReadAsync(buffer, offset, count, cancellationToken);
}

public override ValueTask<int> ReadAsync(Memory<byte> buffer,
CancellationToken cancellationToken = default)
{
EnsureStreamIsOpen();
return underlyingStream.ReadAsync(buffer, cancellationToken);
}

[MemberNotNull(nameof(underlyingStream))]
private void EnsureStreamIsOpen()
{
underlyingStream ??= file.OpenReadStream(maxAllowedSize);
}

protected override void Dispose(bool disposing)
{
if (isDisposed)
{
return;
}

underlyingStream?.Dispose();
isDisposed = true;

base.Dispose(disposing);
}

public override int Read(byte[] buffer, int offset, int count)
=> throw new NotSupportedException();

public override long Seek(long offset, SeekOrigin origin)
=> throw new NotSupportedException();

public override void SetLength(long value)
=> throw new NotSupportedException();

public override void Write(byte[] buffer, int offset, int count)
=> throw new NotSupportedException();
}
30 changes: 16 additions & 14 deletions 5.0/BlazorSample_Server/Pages/file-uploads/FileUpload2.razor
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
@page "/file-upload-2"
@using System.Linq
@using System.Net.Http.Headers
@using System.Text.Json
@using Microsoft.Extensions.Logging
@inject IHttpClientFactory ClientFactory
@inject ILogger<FileUpload2> Logger

<h1>Upload Files</h1>
<h1>File Upload Example 2</h1>

<p>
This example requires a backend server API to function. For more information,
see the <em>Upload files to a server</em> section
of the <em>ASP.NET Core Blazor file uploads</em> article.
</p>

<p>
<label>
Expand Down Expand Up @@ -57,7 +62,7 @@
private async Task OnInputFileChange(InputFileChangeEventArgs e)
{
shouldRender = false;
long maxFileSize = 1024 * 15;
int maxFileSize = 1024 * 15;
var upload = false;

using var content = new MultipartFormDataContent();
Expand All @@ -71,13 +76,8 @@
{
files.Add(new() { Name = file.Name });

var memoryStream = new MemoryStream();
var browserFileStream = file.OpenReadStream(maxAllowedSize: maxFileSize);
await browserFileStream.CopyToAsync(memoryStream);
memoryStream.Position = 0;

var fileContent = new StreamContent(memoryStream);

var stream = new LazyBrowserFileStream(file, maxFileSize);
var fileContent = new StreamContent(stream);
fileContent.Headers.ContentType =
new MediaTypeHeaderValue(file.ContentType);

Expand Down Expand Up @@ -126,7 +126,10 @@
var newUploadResults = await JsonSerializer
.DeserializeAsync<IList<UploadResult>>(responseStream, options);

uploadResults = uploadResults.Concat(newUploadResults).ToList();
if (newUploadResults is not null)
{
uploadResults = uploadResults.Concat(newUploadResults).ToList();
}
}
}

Expand All @@ -136,12 +139,11 @@
private static bool FileUpload(IList<UploadResult> uploadResults,
string fileName, ILogger<FileUpload2> logger, out UploadResult result)
{
result = uploadResults.SingleOrDefault(f => f.FileName == fileName);
result = uploadResults.SingleOrDefault(f => f.FileName == fileName) ?? new();

if (result is null)
if (!result.Uploaded)
{
logger.LogInformation("{FileName} not uploaded (Err: 5)", fileName);
result = new();
result.ErrorCode = 5;
}

Expand Down
83 changes: 83 additions & 0 deletions 6.0/BlazorSample_Server/LazyBrowserFileStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Microsoft.AspNetCore.Components.Forms;
using System.Diagnostics.CodeAnalysis;

namespace BlazorSample;

internal sealed class LazyBrowserFileStream : Stream
{
private readonly IBrowserFile file;
private readonly int maxAllowedSize;
private Stream? underlyingStream;
private bool isDisposed;

public override bool CanRead => true;

public override bool CanSeek => false;

public override bool CanWrite => false;

public override long Length => file.Size;

public override long Position
{
get => underlyingStream?.Position ?? 0;
set => throw new NotSupportedException();
}

public LazyBrowserFileStream(IBrowserFile file, int maxAllowedSize)
{
this.file = file;
this.maxAllowedSize = maxAllowedSize;
}

public override void Flush()
{
underlyingStream?.Flush();
}

public override Task<int> ReadAsync(byte[] buffer, int offset, int count,
CancellationToken cancellationToken)
{
EnsureStreamIsOpen();

return underlyingStream.ReadAsync(buffer, offset, count, cancellationToken);
}

public override ValueTask<int> ReadAsync(Memory<byte> buffer,
CancellationToken cancellationToken = default)
{
EnsureStreamIsOpen();
return underlyingStream.ReadAsync(buffer, cancellationToken);
}

[MemberNotNull(nameof(underlyingStream))]
private void EnsureStreamIsOpen()
{
underlyingStream ??= file.OpenReadStream(maxAllowedSize);
}

protected override void Dispose(bool disposing)
{
if (isDisposed)
{
return;
}

underlyingStream?.Dispose();
isDisposed = true;

base.Dispose(disposing);
}

public override int Read(byte[] buffer, int offset, int count)
=> throw new NotSupportedException();

public override long Seek(long offset, SeekOrigin origin)
=> throw new NotSupportedException();

public override void SetLength(long value)
=> throw new NotSupportedException();

public override void Write(byte[] buffer, int offset, int count)
=> throw new NotSupportedException();
}
21 changes: 10 additions & 11 deletions 6.0/BlazorSample_Server/Pages/file-uploads/FileUpload2.razor
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
@page "/file-upload-2"
@using System.Linq
@using System.Net.Http.Headers
@using System.Text.Json
@using Microsoft.Extensions.Logging
@inject IHttpClientFactory ClientFactory
@inject ILogger<FileUpload2> Logger

<h1>Upload Files</h1>
<h1>File Upload Example 2</h1>

<p>
This example requires a backend server API to function. For more information,
see the <em>Upload files to a server</em> section
of the <em>ASP.NET Core Blazor file uploads</em> article.
</p>

<p>
<label>
Expand Down Expand Up @@ -57,7 +61,7 @@
private async Task OnInputFileChange(InputFileChangeEventArgs e)
{
shouldRender = false;
long maxFileSize = 1024 * 15;
int maxFileSize = 1024 * 15;
var upload = false;

using var content = new MultipartFormDataContent();
Expand All @@ -71,13 +75,8 @@
{
files.Add(new() { Name = file.Name });

var memoryStream = new MemoryStream();
var browserFileStream = file.OpenReadStream(maxAllowedSize: maxFileSize);
await browserFileStream.CopyToAsync(memoryStream);
memoryStream.Position = 0;

var fileContent = new StreamContent(memoryStream);

var stream = new LazyBrowserFileStream(file, maxFileSize);
var fileContent = new StreamContent(stream);
fileContent.Headers.ContentType =
new MediaTypeHeaderValue(file.ContentType);

Expand Down
83 changes: 83 additions & 0 deletions 7.0/BlazorSample_Server/LazyBrowserFileStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Microsoft.AspNetCore.Components.Forms;
using System.Diagnostics.CodeAnalysis;

namespace BlazorSample;

internal sealed class LazyBrowserFileStream : Stream
{
private readonly IBrowserFile file;
private readonly int maxAllowedSize;
private Stream? underlyingStream;
private bool isDisposed;

public override bool CanRead => true;

public override bool CanSeek => false;

public override bool CanWrite => false;

public override long Length => file.Size;

public override long Position
{
get => underlyingStream?.Position ?? 0;
set => throw new NotSupportedException();
}

public LazyBrowserFileStream(IBrowserFile file, int maxAllowedSize)
{
this.file = file;
this.maxAllowedSize = maxAllowedSize;
}

public override void Flush()
{
underlyingStream?.Flush();
}

public override Task<int> ReadAsync(byte[] buffer, int offset, int count,
CancellationToken cancellationToken)
{
EnsureStreamIsOpen();

return underlyingStream.ReadAsync(buffer, offset, count, cancellationToken);
}

public override ValueTask<int> ReadAsync(Memory<byte> buffer,
CancellationToken cancellationToken = default)
{
EnsureStreamIsOpen();
return underlyingStream.ReadAsync(buffer, cancellationToken);
}

[MemberNotNull(nameof(underlyingStream))]
private void EnsureStreamIsOpen()
{
underlyingStream ??= file.OpenReadStream(maxAllowedSize);
}

protected override void Dispose(bool disposing)
{
if (isDisposed)
{
return;
}

underlyingStream?.Dispose();
isDisposed = true;

base.Dispose(disposing);
}

public override int Read(byte[] buffer, int offset, int count)
=> throw new NotSupportedException();

public override long Seek(long offset, SeekOrigin origin)
=> throw new NotSupportedException();

public override void SetLength(long value)
=> throw new NotSupportedException();

public override void Write(byte[] buffer, int offset, int count)
=> throw new NotSupportedException();
}
Loading