Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- image in current folder not deleted upon new upload
- 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;
- }
- }
- }
- }
- 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();
- // 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;
- System.IO.File.Delete(Convert.ToString(reader[0]));
- System.IO.File.Delete(Server.MapPath(Convert.ToString(reader[0])));
Add Comment
Please, Sign In to add comment