gpt4 book ai didi

c# - Server.MapPath 给出了错误的路径,在 IIS 服务器上运行时不支持异常 "The given path' s 格式?

转载 作者:太空宇宙 更新时间:2023-11-03 14:25:25 25 4
gpt4 key购买 nike

目前,我正在使用以下代码将文件保存到我网站上的目录中。

//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/");

关于c# - Server.MapPath 给出了错误的路径,在 IIS 服务器上运行时不支持异常 "The given path' s 格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4221618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com