现在,我正在使用它来允许将文件移动到我们的服务器:
filUpload.PostedFile.SaveAs(Server.MapPath("~/Images/" + filUpload.FileName));
但是,当我不上传文件时,它会给我一个错误:
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:****DocumentsVisual Studio 2010WebSites**Images'.
设置为可选,以便用户不必上传文件?
非常感谢
只需检查filUpload.FileName
是否不为空。仅当不是时上传。
.NET 4.0:
if(!string.IsNullOrWhiteSpace(filUpload.FileName))
{
filUpload.PostedFile.SaveAs(Server.MapPath("~/Images/" + filUpload.FileName));
}
.NET <4.0:
if(!string.IsNullOrEmpty(filUpload.FileName))
{
filUpload.PostedFile.SaveAs(Server.MapPath("~/Images/" + filUpload.FileName));
}