我在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