我在IIS 7.5中有一个网站,并尝试将文件上载到位于另一个驱动器上的“文件”文件夹中。我添加了虚拟文件夹文件到wwwroot文件夹。IIS中的IEnvironment.WebRootPath和虚拟目录
该虚拟文件夹的路径是d:\文件
我在控制器上载代码:
_env变量是IHostingEnvironment和构造注射。
var filename = _env.WebRootPath + [email protected]"\files\{model.JobID}.{model.FileExt}";
using (FileStream fs = System.IO.File.Create(filename))
{
AttachFile.CopyTo(fs);
fs.Flush();
}
它在本地工作,因为我的机器上有物理文件夹wwwroot \ files。不过,这并不在生产服务器上运行,并得到了以下错误:
An unhandled exception has occurred: Could not find a part of the path 'C:\inetpub\Mysite\wwwroot\files\111214.png'.
但我已经拿到了另一个网站的ASPNET 4.6.1和我以前使用Server.Mappath上传的文件。它可以在该网站上运行,我可以成功上传文件。
string SaveLocation = string.Format(@"{0}\{1}{2}", Server.MapPath("files"), JobID, FileExt);
根据这篇文章,它说和使用Server.Mappath是WebRootPath类似。 http://www.mikesdotnetting.com/article/302/server-mappath-equivalent-in-asp-net-core
但是,在我看来,WebRootPath只给了我们网站的根路径。它不接受参数来评估给定的URL,如Server.MapPath。
您能否告诉我如何上传生产服务器上的文件,而不是在我的.Net Core应用程序中硬编码物理路径?
更新1:
这是我迄今为止达到最佳的...我们可以访问或创建另一个虚拟URL指向这样其他位置。它可以通过http://mysite/Files/abc.jpg
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(@"D:\Files"),
RequestPath = new PathString("/Files"),
});
然而,这仅仅是只读的网址,我不能用“/Files/newfile.jpg”或Path.Combine上传到这个路径(_env .WebRootPath,“newfile.jpg”),因为它没有物理存在。
WebRootPath不知道关于IIS vdirs,你必须通过IWebHostBuilder.UseWebRoot(mywebroot) – Tratcher
直接配置它你知道如何解决这个问题吗? – Ruchan
@Ruchan您可以直接使用“D:\ mySharedfiles \”代替IIS虚拟文件夹 – TTCG