Blazor Server InputFile 无法在移动设备上运行
Blazor Server InputFile won't work in mobile
您好,我在 Blazor Server (.NET 6) 中使用 InputFile 组件,它在桌面浏览器中非常有用,但在移动设备中,它就是行不通!
代码如下:
<div class="col-md-12">
<label class="form-label">
@Localizer["pet-information-photograph"]
<button type="button" class="btn btn-outline-modal f-15 rounded-circle font-weight-bold white btn-sm" @onclick="@(() => ModalPetPhotograph())">?</button>
</label>
<InputFile class="form-control" OnChange="ProcessPhotographs" accept=".png,.pdf" />
<small>@Localizer["page-rus-petInformation-fileValidateSize"]</small>
</div>
和方法:
async Task ProcessPhotographs(InputFileChangeEventArgs e)
{
photographBase64 = string.Empty;
if (e.FileCount > 0)
{
foreach (var img in e.GetMultipleFiles(1))
{
var arrbytes = new byte[img.Size];
try
{
if (img.Size < 1024000 && (img.ContentType.Contains("png") || img.ContentType.Contains("jpg") || img.ContentType.Contains("pdf")))
{
await img.OpenReadStream(1024000).ReadAsync(arrbytes);
photographBase64 = $"data:{img.ContentType};base64,{Convert.ToBase64String(arrbytes)}";
}
else
{
ToastService.ShowWarning(Localizer["page-petInformation-fileValidateSize"], Localizer["toast-warning"]);
isLoading = false;
return;
}
}
catch (Exception ex)
{
logger.LogError(ex, ex.Message);
}
}
}
else
{
ToastService.ShowWarning(Localizer["msg-toast-photograph"], Localizer["rus-toast-warning"]);
isLoading = false;
return;
}
additionalPet.Photograph = photographBase64;
}
有什么想法,如何使其适用于移动设备?
我发现了问题,结果是我读取文件的方式导致了问题,经过多次尝试,我们描述了错误代码并为此替换它:
using (var ms = new System.IO.MemoryStream())
{
await img.OpenReadStream(1024000).CopyToAsync(ms);
var arrbytes = ms.ToArray();
您好,我在 Blazor Server (.NET 6) 中使用 InputFile 组件,它在桌面浏览器中非常有用,但在移动设备中,它就是行不通!
代码如下:
<div class="col-md-12">
<label class="form-label">
@Localizer["pet-information-photograph"]
<button type="button" class="btn btn-outline-modal f-15 rounded-circle font-weight-bold white btn-sm" @onclick="@(() => ModalPetPhotograph())">?</button>
</label>
<InputFile class="form-control" OnChange="ProcessPhotographs" accept=".png,.pdf" />
<small>@Localizer["page-rus-petInformation-fileValidateSize"]</small>
</div>
和方法:
async Task ProcessPhotographs(InputFileChangeEventArgs e)
{
photographBase64 = string.Empty;
if (e.FileCount > 0)
{
foreach (var img in e.GetMultipleFiles(1))
{
var arrbytes = new byte[img.Size];
try
{
if (img.Size < 1024000 && (img.ContentType.Contains("png") || img.ContentType.Contains("jpg") || img.ContentType.Contains("pdf")))
{
await img.OpenReadStream(1024000).ReadAsync(arrbytes);
photographBase64 = $"data:{img.ContentType};base64,{Convert.ToBase64String(arrbytes)}";
}
else
{
ToastService.ShowWarning(Localizer["page-petInformation-fileValidateSize"], Localizer["toast-warning"]);
isLoading = false;
return;
}
}
catch (Exception ex)
{
logger.LogError(ex, ex.Message);
}
}
}
else
{
ToastService.ShowWarning(Localizer["msg-toast-photograph"], Localizer["rus-toast-warning"]);
isLoading = false;
return;
}
additionalPet.Photograph = photographBase64;
}
有什么想法,如何使其适用于移动设备?
我发现了问题,结果是我读取文件的方式导致了问题,经过多次尝试,我们描述了错误代码并为此替换它:
using (var ms = new System.IO.MemoryStream())
{
await img.OpenReadStream(1024000).CopyToAsync(ms);
var arrbytes = ms.ToArray();