Последовательность действий выглядит следующим образом:
- Клиент Blazor получает заранее назначенный URL-адрес MinIO для загрузки, чтобы не раскрывать учетные данные для корзины.
- Пользователь выбирает файл и Клиент Blazor читает его как StreamContent.
- Затем HttpClient помещает файл в виде потока в экземпляр MinIO.
Код: Выделить всё
System.Net.Http.HttpRequestException: Error while copying content to a stream.
---> System.IO.IOException: Unable to write data to the transport connection: An existing connection was closed by the remote host..
Код: Выделить всё
@page "/fileupload"
@using System.Linq
@using System.Net.Http.Headers
@inject MinIOClient client
@inject ILogger Logger
@inject HttpClient httpClient
@rendermode InteractiveServer
File Upload 2
File Upload Example 2
Upload
@if (file != null)
{
[list]
[*]
File: @file.Name
@if (FileUploadResults(uploadResult, file.Name, Logger, out var result))
{
Stored File Name: @result.StoredFileName
}
else
{
There was an error uploading the file (Error: @result.ErrorCode).
}
[/list]
}
@code {
private File? file;
private UploadResult uploadResult = new();
private bool shouldRender;
string url = string.Empty;
protected override bool ShouldRender() => shouldRender;
private async Task OnInputFileChange(InputFileChangeEventArgs e)
{
shouldRender = false;
long maxFileSize = 1024 * 1024 * 1024;
var selectedFile = e.File;
if (selectedFile != null)
{
try
{
file = new() { Name = selectedFile.Name };
//This gets the presigned MinIO url
var resp = await client.GetUrl();
if (resp is null) return;
using var content = new MultipartFormDataContent();
var fileContent = new StreamContent(selectedFile.OpenReadStream(maxFileSize));
fileContent.Headers.ContentType = new MediaTypeHeaderValue(selectedFile.ContentType);
content.Add(content: fileContent, name: "\"file\"", fileName: selectedFile.Name);
var response = await httpClient.PutAsync(resp, content);
// Assuming the response contains the upload result
// Update the uploadResult based on the response
// For example:
// uploadResult = await response.Content.ReadFromJsonAsync();
// Simulate a successful upload result for demonstration
uploadResult = new UploadResult
{
FileName = selectedFile.Name,
StoredFileName = "stored_" + selectedFile.Name,
Uploaded = true
};
}
catch (Exception ex)
{
Logger.LogInformation("{FileName} not uploaded (Err: 6): {Message}", selectedFile.Name, ex.Message);
uploadResult = new UploadResult
{
FileName = selectedFile.Name,
ErrorCode = 6,
Uploaded = false
};
}
}
shouldRender = true;
}
private static bool FileUploadResults(UploadResult uploadResult, string? fileName, ILogger logger, out UploadResult result)
{
result = uploadResult;
if (!result.Uploaded)
{
logger.LogInformation("{FileName} not uploaded (Err: 5)", fileName);
result.ErrorCode = 5;
}
return result.Uploaded;
}
private class File
{
public string? Name { get; set; }
}
private class UploadResult
{
public string? FileName { get; set; }
public string? StoredFileName { get; set; }
public bool Uploaded { get; set; }
public int ErrorCode { get; set; }
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... -the-conne
Мобильная версия