我在IIS 7.5中有一个网站,并尝试将文件上传到位于另一个驱动器上的Files文件夹中 . 我在 wwwroot 文件夹中添加了名为 files 的虚拟文件夹 .
该虚拟文件文件夹路径是 D:\files
我在控制器中上传的代码:
_env变量是IHostingEnvironment并在Constructor中注入 .
var filename = _env.WebRootPath + $@"\files\{model.JobID}.{model.FileExt}";
using (FileStream fs = System.IO.File.Create(filename))
{
AttachFile.CopyTo(fs);
fs.Flush();
}
它在本地工作,因为我在我的机器中获得了物理文件夹wwwroot \ files . 但它不能在Production服务器上运行并出现以下错误:
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);
但是,在我看来,WebRootPath只为我们提供了网站的RootPath . 它不接受用于评估给定URL的参数,如Server.MapPath .
您能告诉我如何在Production Server上上传文件,而不是在我的.Net Core应用程序中硬编码物理路径?
Update 1:
这是我到目前为止最好的...我们可以通过这种方式访问或创建另一个虚拟URL以指向另一个位置 . 它可以通过http://mysite/Files/abc.jpg访问
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(@"D:\Files"),
RequestPath = new PathString("/Files"),
});
但是,它只是readonly url,我无法通过使用“/Files/newfile.jpg”或Path.Combine(_env.WebRootPath,“newfile.jpg”)上传到此路径,因为它实际上不存在 .