2011-03-25 28 views
1

好的新问题这是我的完整代码我将解释我正在尝试做的事情并对每个部分进行细分,以便您可以看到我想要实现的目标:当前文件夹中的图片在新上传时未被删除

全码:

protected void UploadButton_Click(object sender, EventArgs e) 
{ 
    if (FileUploadControl.HasFile) 
    { 
     try 
     { 
     string theUserId = Session["UserID"].ToString(); 
     OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=x; Password=x;"); 
     cn.Open(); 

     OdbcCommand sc = new OdbcCommand(string.Format("SELECT picturepath FROM Pictures WHERE UserID ='{0}'", theUserId), cn); 
     OdbcDataReader reader = sc.ExecuteReader(); 

     while (reader.Read()) 
     { 
      if (System.IO.File.Exists(Convert.ToString(reader[0]))) 
      { 
      System.IO.File.Delete(Convert.ToString(reader[0])); 
      } 
     } 

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

     OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "','" + fileuploadpaths + "')", cn); 
     cmd.ExecuteNonQuery(); 
     } 
     catch (Exception ex) 
     { 
     StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 
     } 
    } 
    } 
} 

好吧,我试图做的是选择与用户ID那么图片的路径,如果一个用户ID是存在具有相同的userid我尝试上传删除该文件的第一部分是存在(未存储在数据库中,因此IO)此部分不起作用当前用户标识的文件路径名未被删除。

OdbcCommand sc = new OdbcCommand(string.Format("SELECT picturepath FROM Pictures WHERE UserID ='{0}'", theUserId), cn); 
OdbcDataReader reader = sc.ExecuteReader(); 

while (reader.Read()) 
{ 
    if (System.IO.File.Exists(Convert.ToString(reader[0]))) 
    { 
    System.IO.File.Delete(Convert.ToString(reader[0])); 
    } 
} 

第二部分刚插入新的文件上传路径和名称到我的数据库与当前用户ID(这工作)的文件上传到正确的文件夹和其插入到我的数据库。我可以将其更改为UPDATE,而不是插入atm而不是挑剔。

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

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

所以我的问题是,为什么我的if语句不删除已保存的记录在我的数据库当前图片的路径?发生的一切是我的新图像上传到相同的文件夹,但旧的图像仍然存在?

请记住,虽然我不想删除“相同”的文件名,但是一个简单的saveas会覆盖已经在我的代码中的文件,我需要的是我的代码删除当前处于userid特定状态的任何图像当我试图保存新的图片上传文件夹时。

任何想法对代码有一些帮助?

谢谢你们

+0

重读此其挺难protray什么即时试图解释希望任何expereinced程序员就明白了我在做什么:)抱歉的最佳方式,我可以解释它 – 2011-03-25 22:54:19

+0

你有没有试过调试这段代码?感兴趣的是while循环中的if块。有些东西告诉我FileExists失败了。 – 2011-03-25 23:01:58

+0

是或文件删除,因为删除的路径名是这样的:'〜/ userdata/2/uploadedimage/batman-for-facebook.jpg'我试过调试但没有任何报告 – 2011-03-25 23:05:36

回答

3

看你的代码,相信SystemDown在评论答案:

当您将文件保存到您使用下面的代码盘:

// Even though you've just calculated the result of Path.GetFileName you 
// redo it here? 
string fileuploadpath = Server.MapPath("~/userdata/" + theUserId 
         + "/uploadedimage/") 
         + Path.GetFileName(FileUploadControl.FileName); 
FileUploadControl.SaveAs(fileuploadpath); 

然后你把它存储在数据库中:

string filenameDB = Path.GetFileName(FileUploadControl.FileName); 
string fileuploadpaths = ("~/userdata/" + theUserId + "/uploadedimage/") + 
         filenameDB; 

但是,当你删除文件哟你就像不执行使用Server.Mappath:

System.IO.File.Delete(Convert.ToString(reader[0])); 

更改该行:

System.IO.File.Delete(Server.MapPath(Convert.ToString(reader[0]))); 

,它应该所有的工作。

+0

我希望我的大脑可以长出一些新的神经元:(我应该已经发现了这个问题。谢谢Zhaph – 2011-03-25 23:42:56

+1

@Garrith Graham没关系,我觉得这很明显,我错过了,另一对眼睛很快就会发现它。 – 2011-03-25 23:44:58

0

你应该使用来使用Server.Mappath发现,如果图像已经存在或不然后再试着删除,即时通讯

相关问题