BlazorInputFile icon indicating copy to clipboard operation
BlazorInputFile copied to clipboard

Immidiately use Image

Open kevinruder opened this issue 5 years ago • 1 comments

Hey Steve, I've been using your library and I think it's great!

I've been wanting to use it recently to immediately display an image on the page.

From what I've been reading I'd need to set src attribute on an tag to be :

"window.URL.createObjectURL(fileObj);"

Where fileObj is the file that is uploaded through the BlazorInputFile.

Do you have any idea to how I might reference the file object that is retrieved with BlazorInputFile ?

kevinruder avatar Oct 28 '19 14:10 kevinruder

@kevinruder You can write the image to a temporary file location and then serve it that way...

public async Task<string> SaveTempFile(byte[] content)
{
     try
          {
              Directory.CreateDirectory("/tmp");
              var tempFileName = Path.GetTempFileName();
              await using (var writer = File.OpenWrite(tempFileName))
              {
                  await writer.WriteAsync(content);
              }
              return Path.GetFileName(tempFileName);
          }
          catch (Exception e)
          {
              _logger.LogError($"Failed to Save File! Error: {e}");
              throw;
          }
}

periapsistech avatar Nov 16 '19 23:11 periapsistech