我需要将上传的文件放到我们可以从服务器访问的公共驱动器上。但它正在提供Additional information: 'M:/uploads/' is a physical path, but a virtual path was expected.
<appSettings>
<add key="DirPath" value="M:/uploads/"/>
</appSettings>
代码
String fileName = Path.GetFileNameWithoutExtension(file.FileName);
String extension = Path.GetExtension(file.FileName);
var directory = Server.MapPath(@ConfigurationManager.AppSettings["DirPath"]
.ToString(CultureInfo.InvariantCulture));
如何访问该路径?
答案 0 :(得分:0)
由于异常明确指出你不能(实际上你不需要)使用MapPath
作为绝对路径。如果要在配置中支持绝对路径,则只需检查该值以查看路径是否为绝对路径:
string directory = @ConfigurationManager.AppSettings["DirPath"]
.ToString(CultureInfo.InvariantCulture)
if(!(path.Contains(":") || path.Contains(@"\\") || path.Contains(@"//"))
// convert virtual to physical path
directory = Server.MapPath();
else