'Showing a image with random filename and type in page

I have used this tutorial https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0 to upload images from user to my own location. This is working fine, and when the user uploads the files I get something like "friIjfdls.m4s". It also works fine downloading them.

Just to give a short explanation of the process:

  • User selects images on his computer and uploads them
  • I check that it is a image
  • I give the file a random name before saving it to the wwwroot folder
  • In DB I store the "correct" name

So when the user wants do download it I retrieve the image and the correct name (from db) and "merge" them.

But now I came out for a problem because I also want to show the images on my page. Is that possible? So I cant use the same process as "download file" because that makes the server to a lot of stuff before sending it to the user and it automatically starts downloading. But I just want to show the image as a static(?) image on the page. How can I do that?



Solution 1:[1]

So I ended up doing it this way:

        FileStream fileStream = ResourcesRepository.GetFileByUrl(imagePath);

        byte[] bytes = new byte[fileStream.Length];
        fileStream.Read(bytes, 0, (int)fileStream.Length);
        this.ImageBase64 = Convert.ToBase64String(bytes);

And this worked. Thank you @J.Salas for the direction.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GerryMM88