0% found this document useful (0 votes)
79 views

Code Snippets - Upload and Download File in

This code uploads a file to a database by inserting the file name, content type, file data, and other metadata into a database table. It then allows the file to be downloaded by retrieving the file data and metadata from the database, incrementing the download counter, and sending the file contents to the response stream.

Uploaded by

Alrence Santiago
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Code Snippets - Upload and Download File in

This code uploads a file to a database by inserting the file name, content type, file data, and other metadata into a database table. It then allows the file to be downloaded by retrieving the file data and metadata from the database, incrementing the download counter, and sending the file contents to the response stream.

Uploaded by

Alrence Santiago
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

protected void Upload(object sender, EventArgs e)

{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "insert into tblFiles1 values (@Name, @ContentType, @Data, @ModCode, @ModTitle, @ModLecturer,
@Downloads)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
cmd.Parameters.AddWithValue("@ModCode", TextBox1.Text);
cmd.Parameters.AddWithValue("@ModTitle", TextBox4.Text);
cmd.Parameters.AddWithValue("@ModLecturer", TextBox3.Text);
cmd.Parameters.AddWithValue("@Downloads", Label4.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void DownloadFile(object sender, EventArgs e)
{
int id = int.Parse((sender as LinkButton).CommandArgument);
byte[] bytes;
string fileName, contentType, counter;
string constr = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;

using (SqlConnection con = new SqlConnection(constr))


{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Name, Data, ContentType, ModCode, ModTitle, ModLecturer, Downloads from tblFiles1 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();
counter = sdr["Downloads"].ToString();
}
con.Close();

con.Open();
int counter1 = Convert.ToInt32(counter);
counter1++;

string strCount = Convert.ToString(counter1);


SqlCommand cmd2 = new SqlCommand("UPDATE tblFiles1 SET Downloads = '"+ strCount +"' where id=@id", con);
cmd2.Parameters.AddWithValue("@id", id);
SqlDataReader rd2 = cmd2.ExecuteReader();
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();
}

You might also like