string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
Stream str = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(str);
Byte[] size = br.ReadBytes((int)str.Length);
using (con = new MySqlConnection(ss))
{
con.Open();
using (cmd = new MySqlCommand("insert into referalprogram(Company_Id,File_Name,Referal_Description,File_Type,File_Data,Add_User_Id,Add_Date)values(@cid,@fname,@rd,@ftype,@fdata,@uid,@adddate)", con))
{
cmd.Parameters.Add(new MySqlParameter("@cid", Session["cid"]));
cmd.Parameters.Add(new MySqlParameter("@fname", filename));
cmd.Parameters.Add(new MySqlParameter("@rd", TextBox1.Text));
cmd.Parameters.Add(new MySqlParameter("@ftype", "application/word"));
cmd.Parameters.Add(new MySqlParameter("@fdata", size));
cmd.Parameters.Add(new MySqlParameter("@uid", Session["adduid"]));
cmd.Parameters.Add(new MySqlParameter("@adddate", curr));
cmd.ExecuteNonQuery();
con.Close();
TextBox1.Text = "";
Label4.Text = "Data has been Uploaded.";
}
}
to download by clicking on a button in gridview
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
int id=Convert.ToInt32(e.CommandArgument);
//LinkButton lnkbtn = sender as LinkButton;
//GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
//int fileid = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());
//string name, type;
using ( con = new MySqlConnection(ss))
{
con.Open();
using (cmd = new MySqlCommand("select File_Name, File_Type, File_Data from referalprogram where Ref_Program_Id=@Id",con))
{
cmd.Parameters.Add(new MySqlParameter("@id", id));
dr = cmd.ExecuteReader();
if (dr.Read())
{
Response.ContentType = dr["File_Type"].ToString();
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["File_Name"] + "\"");
Response.BinaryWrite((byte[])dr["File_Data"]);
Response.End();
}
}
}
}
}
Stream str = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(str);
Byte[] size = br.ReadBytes((int)str.Length);
using (con = new MySqlConnection(ss))
{
con.Open();
using (cmd = new MySqlCommand("insert into referalprogram(Company_Id,File_Name,Referal_Description,File_Type,File_Data,Add_User_Id,Add_Date)values(@cid,@fname,@rd,@ftype,@fdata,@uid,@adddate)", con))
{
cmd.Parameters.Add(new MySqlParameter("@cid", Session["cid"]));
cmd.Parameters.Add(new MySqlParameter("@fname", filename));
cmd.Parameters.Add(new MySqlParameter("@rd", TextBox1.Text));
cmd.Parameters.Add(new MySqlParameter("@ftype", "application/word"));
cmd.Parameters.Add(new MySqlParameter("@fdata", size));
cmd.Parameters.Add(new MySqlParameter("@uid", Session["adduid"]));
cmd.Parameters.Add(new MySqlParameter("@adddate", curr));
cmd.ExecuteNonQuery();
con.Close();
TextBox1.Text = "";
Label4.Text = "Data has been Uploaded.";
}
}
to download by clicking on a button in gridview
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
int id=Convert.ToInt32(e.CommandArgument);
//LinkButton lnkbtn = sender as LinkButton;
//GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
//int fileid = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());
//string name, type;
using ( con = new MySqlConnection(ss))
{
con.Open();
using (cmd = new MySqlCommand("select File_Name, File_Type, File_Data from referalprogram where Ref_Program_Id=@Id",con))
{
cmd.Parameters.Add(new MySqlParameter("@id", id));
dr = cmd.ExecuteReader();
if (dr.Read())
{
Response.ContentType = dr["File_Type"].ToString();
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["File_Name"] + "\"");
Response.BinaryWrite((byte[])dr["File_Data"]);
Response.End();
}
}
}
}
}