c# - 如何使用 blazor 客户端应用程序在服务器上创建新文件?
问题描述
我正在尝试通过单击客户端页面上的按钮在 Web 服务器上创建一个新文件。
浏览器控制台仅显示“WASM:已创建文件”。但不显示有关文件操作的任何错误或警告。
/wwwroot 路径下也没有任何新文件。
Blazor 服务器以管理员身份在 localhost (Win10) 上运行。
Index.razor 内容;
@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.");
}
}
}
解决方案
这是不可能的。您的代码在客户端而不是服务器上运行,并且您无权访问客户端上的文件系统。
如果你想做这样的事情,你需要使用 Blazor 服务器应用而不是 Blazor WebAssembly 应用。