上传发生错误!原因:
System.NotSupportedException: 不支持给定路径的格式。
protected void btnUpload_Click(object sender, EventArgs e)
{
if ((FileUpload1.PostedFile.FileName == "" && FileUpload2.PostedFile.FileName == "") && FileUpload3.PostedFile.FileName == "")
{
this.lblInformation.Text = "请选择文件!";
}
else
{
HttpFileCollection myfiles = Request.Files;
for (int i = 0; i < myfiles.Count; i++)
{
HttpPostedFile mypost = myfiles[i];
try
{
if (mypost.ContentLength > 0)
{
string filepath = mypost.FileName;
string filename = filepath.Substring(filepath.LastIndexOf("//") + 1);
string serverpath = Server.MapPath("images/") + filename;
mypost.SaveAs(serverpath);
this.lblInformation.Text = "上传成功!";
}
}
catch (Exception error)
{
this.lblInformation.Text = "上传发生错误!原因:" + error.ToString();
}
}
}
}
具体错误:
上传发生错误!原因:System.NotSupportedException: 不支持给定路径的格式。 在 System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath) 在 System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath) 在 System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList) 在 System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, AccessControlActions control, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath) 在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) 在 System.IO.FileStream..ctor(String path, FileMode mode) 在 System.Web.HttpPostedFile.SaveAs(String filename) 在 Cal_calUpload.btnUpload_Click(Object sender, EventArgs e)
------解决方案--------------------string serverpath = Server.MapPath("images/") + filename;
打个断点看看这个长啥样。。
------解决方案--------------------上传过程,首先你应该判断上传的本地文件是否存在?其次,设置上传支持文件的格式。然后,注意上传的协议方式(是Http还是ftp等),判断是否能够连接到服务器(能否ping得通)。最后,能够ping得通,上传到相应的服务器。
------解决方案--------------------string serverpath = Server.MapPath("images/") + filename;你写成这个样子试试
string serverpath = Server.MapPath("images/"+ filename+"");
------解决方案--------------------
string serverpath = Server.MapPath("~/images/" + System.IO.Path.GetFileName(mypost.FileName)) ;
mypost.SaveAs(serverpath);