Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
583 views
in Technique[技术] by (71.8m points)

webassembly - Blazor WASM File does not exist (but is readable?)

I have a Blazor WASM PWA that I want to be standalone (if possible).

Currently, I am trying to manipulate a json file under wwwroot

Location of Json File

Now, while I am able to read its contents via HttpClient.GetFromJsonAsync, all other attempts to access the File fail.

What I've tried:

  • Check if the File exists via File.Exists -> returns false
  • Post objects via HttpClient.PostAsJsonAsync -> returns 404 for the Json-File

I know that in WebAssembly, Files are cached/loaded into the browser, so I get why I should be unable to locate the File with a Path like "/datasource-json/Ingredient.json", but why does "GetFromJsonAsync" work with that path then?

Is there something crucial I am missing, and how can I work my way around this? For this particular project, I do not want to use another Api-Project + Entity Framework if possible.

question from:https://stackoverflow.com/questions/65832989/blazor-wasm-file-does-not-exist-but-is-readable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Wasm runs entirely on the browser and so you have literally no file system. If you had any it would be the client machine, but that would be a major security breach.

Why getting the file from http works is because the server provides GET access to all files in the web root.

Why POST does not work is because posting something requires a server endpoint that listens for that data on that URL and can understand the format you send. So, you need some sort of controller that will have a specific URL that will not be the same as the file URL


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...