2011-03-25 58 views
0

错误尝试添加文件名,结束串的:错误试图添加文件名,结束串的:

if (FileUploadControl.HasFile) 
    { 
     try 
     { 
      string theUserId = Session["UserID"].ToString(); 
      OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"); 
      cn.Open(); 
      string filename = Path.GetFileName(FileUploadControl.FileName); 
      string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + filename); 
      // error on this line filename only assignment, call can be used as a statement 
      FileUploadControl.SaveAs(Server.MapPath(fileuploadpath)); 
      StatusLabel.Text = "Upload status: File uploaded!"; 

      OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileuploadpath + "')", cn); 
      cmd.ExecuteNonQuery(); 
     } 

     catch (Exception ex) 
     { 
      StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 

     } 

    } 
} 

     } 

如果我试着这样说:

 string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/"); 
     FileUploadControl.SaveAs(Path.Combine(fileuploadpath, filename)); 

我得到一个MySQL错误的没有文件名添加到路径的末尾(顺便说一句,只是试图保存路径不是图像)虽然技术上我应该仍然能够插入一半的文件路径到SQL中,所以也许这个错误与我的原始上传方法没有关系上面的代码。但显然我仍然需要完整的路径名。

无法上传文件。出现以下错误:错误[42000] [MySQL] [ODBC 3.51驱动程序] [mysqld-5.5.9]您的SQL语法错误;检查与您的MySQL服务器版本相对应的手册,以在第1行'C:\ Users \ Garrith \ Documents \ Visual Studio 2010 \ WebSites \ WebSite1 \ userdata \ 1 \ uplo'附近使用正确的语法。

回答

1

您在相同的字符串上使用两次Server.MapPath。请从任何位置移除它,以便根据服务器映射的路径可能不会再映射。

string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + filename); 
FileUploadControl.SaveAs(Server.MapPath(fileuploadpath)); 

你可能不喜欢这样......

string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + filename); 
FileUploadControl.SaveAs(fileuploadpath); 
+0

好吧,这有助于映射的路径,但我仍然在我的字符串文件的上传路径的末尾+文件名中出现错误? – 2011-03-25 10:57:48

+0

你能解释一下这个错误吗?你现在面临的那个错误的细节是什么? – 2011-03-26 08:57:52

0

因为你路径中的第一个字符是〜而得到这个错误。你必须做的是尝试删除这个字符,然后将字符串保存到数据库中。

OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileuploadpath.Substring(1,fileuploadpath.Length - 1) + "')", cn); 
+0

我能救〜通过MySQL的,所以我不就没问题认为它 – 2011-03-25 10:59:30

0

其确定管理这个做我自己:

  //string filename = Path.GetFileName(FileUploadControl.FileName); 
      string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + Path.GetFileName(FileUploadControl.FileName); 
      FileUploadControl.SaveAs(fileuploadpath); 
      StatusLabel.Text = "Upload status: File uploaded!"; 

      OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileuploadpath + "')", cn); 
      cmd.ExecuteNonQuery(); 
     } 

     catch (Exception ex) 
     { 
      StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 

     } 

    } 
} 

     }