Couldn't load json/xml/text file in blazor webassembly

Rikam Palkar 66 Reputation points MVP
2022-04-06T18:04:45.48+00:00

I have Blazor WebAssebly project, In which I am trying to load json file from local machine. And I am always getting file could not found exception.

string path = @"D:\SingleObject.json";
using StreamReader file = File.OpenText(path);

The output of above snippet is, 'Could not find file 'D:\\SingleObject.json'.'

So I tested with this bool fileExist = File.Exists(path); but same story, value of this boolean is always false.

what interesting is this same code works with console app, or Blazor Server app or WPF but for Blazor WebAssembly.
Note: Issue is with file regardless of it's type, I got same exception with .xml and .txt file.

Developer technologies .NET Blazor
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-04-07T02:22:02.19+00:00

    Hi @Rikam Palkar ,

    Agree with AgaveJoe, for security reason, the Blazor WebAssembly application restricted to the capabilities of the browser. If you want to load the client file in Blazor WebAssembly application, you should upload the file using the upload input. If you want to access the file in Blazor WebAssembly application, you could add the json file in the wwwroot folder, then access the file via the http request. Like this:

    190744-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.

    Best regards,
    Dillion

    2 people found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2022-04-06T21:01:33.183+00:00

    I have Blazor WebAssebly project, In which I am trying to load json file from local machine.

    This requirement is not possible due to browser security. The user must select the file using a standard file upload input.

    what interesting is this same code works with console app, or Blazor Server app or WPF but for Blazor WebAssembly.

    It only seems to work because your development machine is both the client and server. Once, deployed to an actual server the file will not be found because the file is on the client machine not the server.

    1 person found this answer helpful.
    0 comments No comments

  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-04-06T22:26:23.917+00:00

    web assemblies run in a sandbox, and do not have access to the filesystem (or network for that matter). You can use javascript interop to use the browsers file api, which has some restrictions.

    https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API

    if you want to access files on the server like server blazer, just use a webapi to access.

    1 person found this answer helpful.
    0 comments No comments

  3. Rikam Palkar 66 Reputation points MVP
    2022-04-07T06:37:24.543+00:00

    @AgaveJoe , @Bruce (SqlWork.com) and @Anonymous
    Thank you so much guys for quick response.
    Also Joe thank you clearing out the confusion.
    I followed the steps @Anonymous mentioned, and was able to load the file from wwwroot

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.