【问题标题】:overwrite upon fileupload文件上传时覆盖
【发布时间】:2023-03-17 03:03:01
【问题描述】:

大家好,有没有办法在文件上传时覆盖文件夹的任何内容,即使文件名不一样?我只希望在任何给定的时间存储一张图像,但我无法知道用户可能上传的文件名,那么您将如何在下面的代码中覆盖?

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

            }

        }
    }

}

【问题讨论】:

  • 为什么不在复制前删除文件夹的内容?
  • @Pekka,这是我书中的答案。

标签: c# asp.net mysql sql html


【解决方案1】:

当您说覆盖它时,为什么不能删除旧文件?这可以通过对图像类型的目录列表进行过滤来完成,或者如果图像是其中唯一的文件,则可以通过清除整个目录来完成。

您更好的选择是从数据库中提取文件名,因为您已经存储了文件名以与用户 ID 关联。这样当用户上传新文件时,您可以调用当前用户记录并删除关联文件,并在新文件上传完成后更新图片记录。

最后,第三种选择是将文件作为二进制值存储在数据库中。然后每次上传图片时,只需将图片更新到用户图片记录即可。

[编辑:更多细节]

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


//
//Something like this 
//


                OdbcCommand sc = new OdbcCommand(string.format("SELECT picturepath FROM Pictures WHERE UserID ='{0}'", theUserId), cn);
                OdbcDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                      if (System.IO.File.Exists(reader[0]))
                      {
                           System.IO.File.Delete(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;

            }

        }
    }

}

但是,我必须警告你。使用这样的代码是非常草率和不安全的。例如,在代码中使用 sql 查询字符串会使您的站点受到 SQL 注入攻击。最好使用 LINQ to SQL 或 Entity to SQL 之类的东西。除了使读取和向数据库写入数据变得更加简单之外,它还提供了防止 SQL 注入的数据清理功能。

每次需要时从连接字符串创建 OdbcConnection 对象也是一个缓慢的过程。您可能希望创建一个延迟加载单例,它为每个会话或应用程序实例返回一个 OdbcConnection 实例。

如果出于某种原因您确实想要创建 OdbcConnection 对象的各个实例,您可能需要查看 using 函数。

using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=x; Password=x;"))
{

    // DO some Work here with the OdbcConnection

}  // Automatically close and dispose of the connection object to avoid memory leaks.

【讨论】:

  • 有什么建议吗?如果我可以使用数据库找到当前图像并执行某种删除而不是插入进行更新(按权利我应该显示该代码)我的另一个选项将在页面加载时查找任何路径存储在我的数据库中并删除它,但我只是不知道代码如何执行此操作或您提到的任何选项? :寻找
  • 更新了响应以包含代码示例。我在文本编辑器中输入了这个,因此没有测试它。如果您要使用我的代码,请确保正确测试所有内容。
猜你喜欢
  • 2011-08-27
  • 1970-01-01
  • 2023-02-03
  • 1970-01-01
  • 2011-12-25
  • 2016-03-18
  • 2018-05-20
  • 2012-12-06
  • 2011-05-14
相关资源
最近更新 更多