![](/img/trans.png)
[英]StreamReader Server.MapPath - Physical path given, virtual path expected
[英]Server.MapPath gives wrong path, exception “The given path's format is not supported” when running on IIS server?
目前,我正在使用以下代碼將文件保存到網站上的目錄中。
//save the file to the server
String savePath = Server.MapPath("..\\temp\\") + file;
FileUpload.PostedFile.SaveAs(savePath);
當我在計算機上運行應用程序時,它返回路徑:
“ E:\\ dotnet \\ Project \\ Implementation \\ Source Code \\ Project \\ Project.UI \\ temp \\ Sample.csv”
但是,在真實服務器上運行時,它將引發異常:
System.NotSupportedException: The given path's format is not supported.
我希望擁有的預期路徑是“ C:\\ inetpub \\ wwwroot \\ Project \\ temp”。
我做的事正確與否嗎? 如果不是,那么有人可以向我解釋正確的方法嗎?
Server.MapPath將虛擬路徑映射到服務器上的物理路徑。 如果將反斜杠替換為正斜杠,則應該可以:
Server.MapPath("../temp/")
您還可以使用根相對路徑:
Server.MapPath("~/temp/");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.