好的新问题这是我的完整代码我将解释我正在尝试做什么并对每个部分进行细分,以便您可以看到我正在努力实现的目标:
完整代码:
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与我尝试上传时具有相同的用户ID删除存在的文件(不存储在数据库中,因此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而不是插入,但不管它是否要繁琐。
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 :(得分: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);
然后将其存储在DB中:
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])));
它应该都可以。
答案 1 :(得分:0)
您应该使用server.mappath来查找图片是否已经存在然后尝试删除该im