Reputation: 3
I am trying to create a new file on the web server by clicking a button on the client page.
Browser console shows only "WASM: File created." but does not show any errors or warnings about the file operations.
There are not any new files under the path /wwwroot either.
Blazor server is running on localhost (Win10) as administrator.
Index.razor content;
@page "/"
<h1>Demo1</h1>
<button type="submit" @onclick="CreateFile.Run">Create A New File</button>
@code
{
public class CreateFile
{
public static void Run()
{
System.IO.File.WriteAllText("test.txt", "hello world");
System.IO.File.WriteAllText(@"C:\VS Code Projects\Demo1\Client\wwwroot\samplefile.txt", "content 123");
Console.WriteLine("File created.");
}
}
}
Upvotes: 0
Views: 3922
Reputation:
This message: "WASM: File created." indicates that you're using a Blazor WebAssembly App, right? The browser is not supposed to allow you to create a file on the client. This is only a message produced by your code.
Understand this: When you hit the button a normal post back occurs because the type of the button is 'submit'. After the return to the calling page, I guess, a click event is triggered (not sure but it may be so), and the code in the run method is executed to display the message "WASM: File created."
Understand this: You've created a Blazor WebAssembly App server hosted. This is a Single Page Application, and it should communicate with your server via a Web Api or SignleR. You must not use post backs, and you need to learn how the two modes of executions of Blazor work.
Upvotes: 0
Reputation: 8521
This isn't possible. Your code is running on the client not on the server and you don't have access to the file system on the client.
If you want to do things like this you need to use a Blazor Server App rather than a Blazor WebAssembly App.
Upvotes: 3