如何在没有控制器的 Blazor 应用程序剃刀页面中找到 wwwroot 的物理路径?
How can I find the physical path of wwwroot, in a Blazor app razor page without a controller?
我正在尝试在我的 blazor 应用程序(blazor 服务器应用程序)的页面中使用以下代码
@inject IWebHostEnvironment WebhostEnvironment
public IWebHostEnvironment _environment { get; set; }
var uploads = Path.Combine(_environment.WebRootPath, "uploads");
//string uploads = @"z:\Downloads";
path = uploads + "\" + file.FileInfo.Name;
FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write);
file.Stream.WriteTo(filestream);
filestream.Close();
file.Stream.Close();
将文件保存在 wwwroot 文件夹内的上传文件夹中,但是当我执行代码时,我得到“对象引用未设置为对象的实例”。
问题
1.what 导致错误?
2.Is 将上传文件保存在 wwwroot 子文件夹中是最佳途径吗?
3. 在网络服务器环境中,是否可以将网络路径简单地设置在字符串变量中作为替代方案?当我取消注释上面的字符串上传行时,上传成功到我家庭网络上的指定网络驱动器。
感谢您提供的任何信息...
我假设我们谈论的是“Blazor 服务器端”。
物理 wwwroot 文件夹可以通过以下方式找到:
string rootpath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "wwwroot");
将上传的文件保存到 wwwroot 的子目录有时不是最优的,因为出于安全原因,这个地方必须是只读的。您的用户是否需要下载回上传的文件?如果没有,wwwroot 之外的临时文件夹会更好。
网络路径?这是可能的,但您的 Web 应用程序应该 运行 在具有此位置读写权限的凭据下。通常,具有受限凭据的 Web 应用程序 运行。
总而言之:您的最后 2 个问题必须考虑安全问题。
我正在尝试在我的 blazor 应用程序(blazor 服务器应用程序)的页面中使用以下代码
@inject IWebHostEnvironment WebhostEnvironment
public IWebHostEnvironment _environment { get; set; }
var uploads = Path.Combine(_environment.WebRootPath, "uploads");
//string uploads = @"z:\Downloads";
path = uploads + "\" + file.FileInfo.Name;
FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write);
file.Stream.WriteTo(filestream);
filestream.Close();
file.Stream.Close();
将文件保存在 wwwroot 文件夹内的上传文件夹中,但是当我执行代码时,我得到“对象引用未设置为对象的实例”。
问题 1.what 导致错误? 2.Is 将上传文件保存在 wwwroot 子文件夹中是最佳途径吗? 3. 在网络服务器环境中,是否可以将网络路径简单地设置在字符串变量中作为替代方案?当我取消注释上面的字符串上传行时,上传成功到我家庭网络上的指定网络驱动器。
感谢您提供的任何信息...
我假设我们谈论的是“Blazor 服务器端”。
物理 wwwroot 文件夹可以通过以下方式找到:
string rootpath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "wwwroot");
将上传的文件保存到 wwwroot 的子目录有时不是最优的,因为出于安全原因,这个地方必须是只读的。您的用户是否需要下载回上传的文件?如果没有,wwwroot 之外的临时文件夹会更好。
网络路径?这是可能的,但您的 Web 应用程序应该 运行 在具有此位置读写权限的凭据下。通常,具有受限凭据的 Web 应用程序 运行。
总而言之:您的最后 2 个问题必须考虑安全问题。