protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
}
protected void UploadFile(object sender, EventArgs e)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void DownloadFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}
protected void DeleteFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
File.Delete(filePath);
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void SaveAllFiles(object sender, EventArgs e)
{
string fileNames = string.Empty;
foreach (GridViewRow row in this.GridView1.Rows)
{
fileNames += row.Cells[0].Text + ",";
}
string files = fileNames.Remove(fileNames.Length - 1);
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
string sqlStatment = "UPDATE FilesUploaded SET FileName = @FileName WHERE Id =@Id";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
con.Open();
cmd.Parameters.AddWithValue("@FileName", files);
cmd.Parameters.AddWithValue("@Id", Convert.ToInt32(this.txtId.Text.Trim()));
cmd.ExecuteNonQuery();
con.Close();
}
}
}