从 `BlazorInputFile.IFileStream` 获取 `byte[]` 的最简单方法是什么
What is the easiest way to obtain `byte[]` from `BlazorInputFile.IFileStream`
我有一个文件,用户将 select 在他们的浏览器中使用 BlazorInputFile as expounded by Steve Sanderson。
一旦 selected 我想使用 System.Security.Cryptography.MD5
计算文件的校验和,类似于 Calculate MD5 checksum for a file[=31= 的答案中描述的内容].
但是,当我尝试这个时遇到了 System.NotSupportedException
:
private string GetMd5ForFile(IFileListEntry file)
{
using (var md5 = MD5.Create())
{
return Convert.ToBase64String(md5.ComputeHash(this.file.Data));
}
}
一些异常详细信息是:
> Message: "Synchronous reads are not supported"
> Source : "BlazorInputFile"
> Stack : "at BlazorInputFile.FileListEntryStream.Read(Byte[] buffer, Int32 offset, Int32 count)"
我知道 ComputeHash()
采用 byte
的数组。到目前为止,我已经尝试将 BlazorInputFile 的流转换为熟悉的类型,或者使用它自己的方法将字节读取到数组 FileStream
,但没有成功。
我最终这样做了:
private async Task<string> GetMd5ForFile(IFileListEntry file) {
using (var md5 = MD5.Create()) {
var data = await file.ReadAllAsync();
return Convert.ToBase64String(md5.ComputeHash(data.ToArray()));
}
}
我有一个文件,用户将 select 在他们的浏览器中使用 BlazorInputFile as expounded by Steve Sanderson。
一旦 selected 我想使用 System.Security.Cryptography.MD5
计算文件的校验和,类似于 Calculate MD5 checksum for a file[=31= 的答案中描述的内容].
但是,当我尝试这个时遇到了 System.NotSupportedException
:
private string GetMd5ForFile(IFileListEntry file)
{
using (var md5 = MD5.Create())
{
return Convert.ToBase64String(md5.ComputeHash(this.file.Data));
}
}
一些异常详细信息是:
> Message: "Synchronous reads are not supported"
> Source : "BlazorInputFile"
> Stack : "at BlazorInputFile.FileListEntryStream.Read(Byte[] buffer, Int32 offset, Int32 count)"
我知道 ComputeHash()
采用 byte
的数组。到目前为止,我已经尝试将 BlazorInputFile 的流转换为熟悉的类型,或者使用它自己的方法将字节读取到数组 FileStream
,但没有成功。
我最终这样做了:
private async Task<string> GetMd5ForFile(IFileListEntry file) {
using (var md5 = MD5.Create()) {
var data = await file.ReadAllAsync();
return Convert.ToBase64String(md5.ComputeHash(data.ToArray()));
}
}