开发者

image in current folder not deleted upon new upload

Ok new problem this is my full code I will explain what I'm trying to do and segment each part so you can see what it is I'm trying to achieve:

Full Code:

 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/") +
       开发者_JAVA百科                         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;
      }
    }
  }
}

Ok the first part I'm trying to do is select the picture path related to the userid then if a userid is there with the same userid as im trying to upload delete the file that exists(not stored in database hence the IO) this part doesn't work the file pathname of the current userid isn't being deleted.

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]));
  }  
}

The second part just inserts the new file upload path and name into my database related to the current userid (this works) the file is uploaded to the correct folder and its inserted into my database. I could change this to UPDATE rather than insert but atm its either or not to fussy.

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();

So my problem is why is my if statement not deleting the currently held record in my database for the path of the current image? All that happens is my new image is uploaded into the same folder but the old image still remains?

Remember though I'm not trying to delete the "same" file name a simple saveas would overwrite it which is already in my code, what I need is for my code to delete any image that is currently in the userid specific folder when I'm trying to save the new image upload.

Any thoughts some help on the code?

Thanks guys


Looking at your code, I believe SystemDown has the answer in the comments:

When you save the file to disk you use the following code:

// 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);

Then you store it in the DB as:

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

However when you delete the file you're not performing Server.MapPath:

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

Change that line to:

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

And it should all work.


you should use the server.mappath to find that if image is already exist or not then try to delete that im

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜