[英]Server.MapPath gives wrong path, exception “The given path's format is not supported” when running on IIS server?
Currently, I'm using the codes below to save a file to a directory on my website. 目前,我正在使用以下代码将文件保存到网站上的目录中。
//save the file to the server
String savePath = Server.MapPath("..\\temp\\") + file;
FileUpload.PostedFile.SaveAs(savePath);
When I'm running the application on my computer, it returns the path: 当我在计算机上运行应用程序时,它返回路径:
"E:\\dotnet\\Project\\Implementation\\Source Code\\Project\\Project.UI\\temp\\Sample.csv" “ E:\\ dotnet \\ Project \\ Implementation \\ Source Code \\ Project \\ Project.UI \\ temp \\ Sample.csv”
However, when running on a real server, it throws exception: 但是,在真实服务器上运行时,它将引发异常:
System.NotSupportedException: The given path's format is not supported.
The expected path that I would like to have is "C:\\inetpub\\wwwroot\\Project\\temp". 我希望拥有的预期路径是“ C:\\ inetpub \\ wwwroot \\ Project \\ temp”。
Am I doing things correctly or not? 我做的事正确与否吗? If not then can someone explain to me the correct way to do this? 如果不是,那么有人可以向我解释正确的方法吗?
Server.MapPath maps a virtual path to a physical one on the server. Server.MapPath将虚拟路径映射到服务器上的物理路径。 If you replace your backslashes with forward-slashes you should be ok: 如果将反斜杠替换为正斜杠,则应该可以:
Server.MapPath("../temp/")
You can also use the root relative path: 您还可以使用根相对路径:
Server.MapPath("~/temp/");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.