Guest User

Untitled

a guest
Sep 3rd, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. image in current folder not deleted upon new upload
  2. protected void UploadButton_Click(object sender, EventArgs e)
  3. {
  4. if (FileUploadControl.HasFile)
  5. {
  6. try
  7. {
  8. string theUserId = Session["UserID"].ToString();
  9. OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=x; Password=x;");
  10. cn.Open();
  11.  
  12. OdbcCommand sc = new OdbcCommand(string.Format("SELECT picturepath FROM Pictures WHERE UserID ='{0}'", theUserId), cn);
  13. OdbcDataReader reader = sc.ExecuteReader();
  14.  
  15. while (reader.Read())
  16. {
  17. if (System.IO.File.Exists(Convert.ToString(reader[0])))
  18. {
  19. System.IO.File.Delete(Convert.ToString(reader[0]));
  20. }
  21. }
  22.  
  23. string filenameDB = Path.GetFileName(FileUploadControl.FileName);
  24. string fileuploadpath = Server.MapPath("~/userdata/" + theUserId +
  25. "/uploadedimage/") +
  26. Path.GetFileName(FileUploadControl.FileName);
  27. FileUploadControl.SaveAs(fileuploadpath);
  28. string fileuploadpaths = ("~/userdata/" + theUserId + "/uploadedimage/") +
  29. filenameDB;
  30. StatusLabel.Text = "Upload status: File uploaded!";
  31.  
  32. OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "','" + fileuploadpaths + "')", cn);
  33. cmd.ExecuteNonQuery();
  34. }
  35. catch (Exception ex)
  36. {
  37. StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
  38. }
  39. }
  40. }
  41. }
  42.  
  43. OdbcCommand sc = new OdbcCommand(string.Format("SELECT picturepath FROM Pictures WHERE UserID ='{0}'", theUserId), cn);
  44. OdbcDataReader reader = sc.ExecuteReader();
  45.  
  46. while (reader.Read())
  47. {
  48. if (System.IO.File.Exists(Convert.ToString(reader[0])))
  49. {
  50. System.IO.File.Delete(Convert.ToString(reader[0]));
  51. }
  52. }
  53.  
  54. string filenameDB = Path.GetFileName(FileUploadControl.FileName);
  55. string fileuploadpath = Server.MapPath("~/userdata/" + theUserId +
  56. "/uploadedimage/") +
  57. Path.GetFileName(FileUploadControl.FileName);
  58. FileUploadControl.SaveAs(fileuploadpath);
  59. string fileuploadpaths = ("~/userdata/" + theUserId + "/uploadedimage/") +
  60. filenameDB;
  61. StatusLabel.Text = "Upload status: File uploaded!";
  62.  
  63. OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "','" + fileuploadpaths + "')", cn);
  64. cmd.ExecuteNonQuery();
  65.  
  66. // Even though you've just calculated the result of Path.GetFileName you
  67. // redo it here?
  68. string fileuploadpath = Server.MapPath("~/userdata/" + theUserId
  69. + "/uploadedimage/")
  70. + Path.GetFileName(FileUploadControl.FileName);
  71. FileUploadControl.SaveAs(fileuploadpath);
  72.  
  73. string filenameDB = Path.GetFileName(FileUploadControl.FileName);
  74. string fileuploadpaths = ("~/userdata/" + theUserId + "/uploadedimage/") +
  75. filenameDB;
  76.  
  77. System.IO.File.Delete(Convert.ToString(reader[0]));
  78.  
  79. System.IO.File.Delete(Server.MapPath(Convert.ToString(reader[0])));
Add Comment
Please, Sign In to add comment