我有这个函数将文件保存到server.mappath。我在第if (!File.Exists(Server.MapPath(existsFile)))
行中遇到的错误是:
'给出物理路径,预期虚拟路径'
代码:
protected void Upload(object sender, EventArgs e)
{
string directoryPath = @"~\Uploads\" + Session["userid"];
if (!Directory.Exists(HttpContext.Current.Server.MapPath(directoryPath)))
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(directoryPath));
if (FileUpload1.HasFile)
{
HttpFileCollection hfc = Request.Files;
for (int i = 0; i <= hfc.Count - 1; i++) // CHECK THE FILE COUNT.
{
HttpPostedFile postedFile = hfc[i];
if (postedFile.ContentLength > 0)
{
string filename = Path.GetFileName(postedFile.FileName);
string contentType = postedFile.ContentType;
string existsFile = Server.MapPath(directoryPath)+"\\" + filename;
if (!File.Exists(Server.MapPath(existsFile)))
postedFile.SaveAs(Server.MapPath(directoryPath) + filename);
}
}
}
LoadUploadedFiles(ref this.GridViewFileList, directoryPath);
}