How to load images/videos directly from server in Blazor WebAssembly?

Daniel Avadanei 41 Reputation points
2021-08-02T10:03:42.757+00:00

I implemented a solution in Blazor webassembly that allows the user to upload an image and store the file on the server and the filepath in the database. When I load the image on client I send the image from the sever to client as bytes and transform the image bytes in a base64 string and display the image. This works well but from what I read, this is not a good practice because the base64 tranformation is increasing the file size by 30% and the images cannot be cached on client. I would want to let the browser download the file from the server path and display the image. My questions are the following:

How do I set the server 'root' location in order for the browser to know where to get the files ? I'm using IWebHostEnvironment on the server to get the local path.
Is there any way to secure the files to allow only the authorised user to download the files from the server ?
Should I add the images in the same folder as the solution or I should make a different folder outside of the solution ?
I'm currently using IIS Server with ASP.NET CORE 5 in a Blazor WebAssembly solution.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,396 questions
0 comments No comments
{count} votes

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,766 Reputation points
    2021-08-02T14:10:21.407+00:00

    You action could return the image as file, then you Blazor code sets an image src the webapi action urls.

    If you want to use the static file handler, be sure to configure security before the static handler. Put the images in a folder under wwwroot.

    The index page should have set the base ref, so you just use /folder/imagepath

    1 person found this answer helpful.
    0 comments No comments

  2. Fei Han - MSFT 306 Reputation points Microsoft Vendor
    2021-08-14T06:04:55.603+00:00

    Is there any way to secure the files to allow only the authorised user to download the files from the server ?

    Hi @Daniel Avadanei , to achieve your requirement of protecting/securing static files, as @Bruce (SqlWork.com) mentioned, you can try to create a controller action method with the [Authorize] attribute to serve those static files by return a FileResult object, like below.

    [HttpGet("/FetchImageAndVideo/{type}/{filename}")]  
    [Authorize]  
    public IActionResult FetchImageAndVideo(string filename, int type)  
    {  
        //...  
        //code logic here  
        //...  
      
        var contentType = type == 1 ? "image/jpeg" : "video/mp4";  
      
        var filePath = Path.Combine(_env.ContentRootPath, "MyImagesAndVideos", $"{filename}");  
      
        if (!System.IO.File.Exists(filePath))  
        {  
            contentType = "image/jpeg";  
            filePath = "default_notfound_image_path_here";  
        }  
      
        return PhysicalFile(filePath, contentType);  
    }  
    

    Accessing a specific file from frontend code, like below.

    src="https://xxxxx/FetchImageAndVideo/2/mytestvideo.mp4"  
    

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    With Regards,
    Fei Han