2025-11-05 14:31:34 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Radzen;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a file which the user selects for upload via <see cref="Radzen.Blazor.RadzenUpload" />.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class FileInfo : IBrowserFile
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates FileInfo.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public FileInfo()
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 12:29:58 +05:00
|
|
|
private IBrowserFile? source;
|
2025-11-05 14:31:34 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates FileInfo with IBrowserFile as source.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source">The source browser file.</param>
|
|
|
|
|
public FileInfo(IBrowserFile source)
|
|
|
|
|
{
|
|
|
|
|
this.source = source;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 12:29:58 +05:00
|
|
|
private string? name;
|
|
|
|
|
private DateTimeOffset? lastModified;
|
|
|
|
|
private string? contentType;
|
2025-11-05 14:31:34 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the name of the selected file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2026-01-07 12:29:58 +05:00
|
|
|
return name ?? source?.Name ?? string.Empty;
|
2025-11-05 14:31:34 +02:00
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
name = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private long size;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the size (in bytes) of the selected file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public long Size
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return size != default(long) ? size : source != null ? source.Size : 0;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
size = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the IBrowserFile source.
|
|
|
|
|
/// </summary>
|
2026-01-07 12:29:58 +05:00
|
|
|
public IBrowserFile? Source => source;
|
2025-11-05 14:31:34 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the LastModified.
|
|
|
|
|
/// </summary>
|
2026-01-07 12:29:58 +05:00
|
|
|
public DateTimeOffset LastModified
|
|
|
|
|
{
|
|
|
|
|
get => lastModified ?? source?.LastModified ?? default;
|
|
|
|
|
set => lastModified = value;
|
|
|
|
|
}
|
2025-11-05 14:31:34 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the ContentType.
|
|
|
|
|
/// </summary>
|
2026-01-07 12:29:58 +05:00
|
|
|
public string ContentType
|
|
|
|
|
{
|
|
|
|
|
get => contentType ?? source?.ContentType ?? string.Empty;
|
|
|
|
|
set => contentType = value;
|
|
|
|
|
}
|
2025-11-05 14:31:34 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Open read stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="maxAllowedSize">The maximum allowed size.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>The stream.</returns>
|
|
|
|
|
public System.IO.Stream OpenReadStream(long maxAllowedSize = 512000, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
2026-01-07 12:29:58 +05:00
|
|
|
if (source == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("No underlying browser file is associated with this FileInfo instance.");
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 14:31:34 +02:00
|
|
|
return source.OpenReadStream(maxAllowedSize, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|