0

I have a file that a user will select in their browser using BlazorInputFile as expounded by Steve Sanderson.

Once selected I want to calculate a checksum for the file using System.Security.Cryptography.MD5, similar to what's described in this answer to Calculate MD5 checksum for a file.

However, I encounter a System.NotSupportedException when I try this:

private string GetMd5ForFile(IFileListEntry file)
{
   using (var md5 = MD5.Create())
   {
      return Convert.ToBase64String(md5.ComputeHash(this.file.Data));
   }
}

Some exception details are:

 > Message: "Synchronous reads are not supported"
 > Source : "BlazorInputFile"
 > Stack  : "at BlazorInputFile.FileListEntryStream.Read(Byte[] buffer, Int32 offset, Int32 count)"

I know that ComputeHash() takes an array of byte. I've so far tried to cast BlazorInputFile's streams to familiar types or to read the bytes to an array with a method of its own FileStream, but without success.

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46

1 Answers1

0

I ended up doing this:


private async Task<string> GetMd5ForFile(IFileListEntry file) {
    using (var md5 = MD5.Create()) {
        var data = await file.ReadAllAsync();
        return Convert.ToBase64String(md5.ComputeHash(data.ToArray()));
    }
}

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
  • 2
    Your answer does not satisfy your own question's title... – Heretic Monkey Jun 28 '20 at 22:23
  • 1
    Indeed. It's the only answer I've got, so far, to the scenario as a whole. It's hoped better information will come along. The title reflects the way the problem presented to me as a working programmer and so might occur to others that way — but neither answer nor title is perfect. Just accepted. "Accepting an answer is not meant to be a definitive and final statement indicating that the question has now been answered perfectly. It simply means that the author received an answer that worked for them personally." Thanks for your input. https://stackoverflow.com/help/accepted-answer – Trevor Reid Jun 29 '20 at 01:22