删除 C# 数据库中上传的文件
本文关键字:文件 数据库 删除 | 更新日期: 2023-09-27 18:21:11
using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select id, Name from FileControl";
cmd.Connection = con;
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
}
}
protected void DownloadFile(object sender, EventArgs e)
{
int id = int.Parse((sender as LinkButton).CommandArgument);
byte[] bytes;
string fileName, contentType;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Name, Data, ContentType from FileControl where id=@id";
cmd.Parameters.AddWithValue("@id", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["Data"];
contentType = sdr["ContentType"].ToString();
fileName = sdr["Name"].ToString();
}
con.Close();
}
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
protected void View(object sender, EventArgs e)
{
int id = int.Parse((sender as LinkButton).CommandArgument);
string embed = "<object data='"{0}{1}'" type='"image/jpg'" width='"500px'" height='"300px'">";
embed += "If you are unable to view file, you can download from <a href = '"{0}{1}&download=1'">here</a>";
embed += " or download <a target = '"_blank'" href = '"http://get.adobe.com/reader/'">Adobe PDF Reader</a> to view the file.";
embed += "</object>";
ltEmbed.Text = string.Format(embed, ResolveUrl("~/FileCS.ashx?Id="), id);
}
protected void DeleteFile(object sender, EventArgs e)
{
int id = int.Parse((sender as LinkButton).CommandArgument);
byte[] bytes;
string fileName, contentType;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "Delete Name, Data, ContentType from FileControl where id=@id";
cmd.Parameters.AddWithValue("@id", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["Data"];
contentType = sdr["ContentType"].ToString();
fileName = sdr["Name"].ToString();
}
con.Close();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/ProfileChange.aspx");
}
}
在这里,查看和下载功能运行良好。我需要删除上传的文件。问题是我没有使用任何文件夹来存储文件 - 我直接使用数据库来存储文件的路径。请帮助我更正删除功能。
DeleteFile
方法具有正确的 SQL 命令,用于从按 id FileControl
的表中删除记录。
DeleteFile
方法的问题在于它是复制、粘贴和重命名的方法DownloadFile
版本,并且DownloadFile
有一个返回数据的 SQL 查询,因此它可以使用以下代码将 FileBlob 或字节列FileControl.Data
到文件中:
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["Data"];
contentType = sdr["ContentType"].ToString();
fileName = sdr["Name"].ToString();
}
您在DeleteFile
中具有相同的代码,但 SQL 语句不会返回任何数据,您正在删除 DeleteFile 中的记录,您需要按照注释中所述@ErikPhilips ExecuteNonQuery()
DeleteFile
修复此代码