upload
Create a table (UploadedFiles) having a column (upload) with datatype nvarchar(max).In the application drag a fileupload control and a buton. In the click event write the below code:
SqlCommand cmm;
SqlConnection con;
SqlDataAdapter da;
string s;
protected void Button2_Click(object sender, EventArgs e){
cmm =
new SqlCommand("Insert into UploadedFiles values(@a)");con =
new SqlConnection("Data Source=Synaro01242;Initial Catalog=Test;uid=sa;Pwd=Password123$");cmm.Connection = con;
con.Open();
da =
new SqlDataAdapter(cmm);
if (FileUpload1.HasFile){
string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
//if (fileExt == ".pdf" || fileExt == ".txt" || fileExt == ".docx")//{// //s = MapPath("img");
s = s + "/" + FileUpload1.FileName;
FileUpload1.SaveAs(s);
//}//else//{// Response.Write("Invalid Upload file format");//}}
cmm.Parameters.AddWithValue("@a",s);cmm.ExecuteNonQuery();
con.Close();
}
Download
In the gridview bind it with the database table (UploadedFiles). Now add a buttonfield named Download, and Command name as "Download"
In Gridview RowCommand Event write the below code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e){
if (e.CommandName == "Download"){
int index = Convert.ToInt32(e.CommandArgument);
string url = GridView1.Rows[index].Cells[0].Text;System.IO.
FileInfo file = new System.IO.FileInfo(url);
if (file.Exists){
Response.Clear();
Response.AppendHeader(
"Content-Disposition:", "attachment; filename=" + file.Name);Response.AppendHeader(
"Content-Length", file.Length.ToString());Response.ContentType =
"application/octet-stream";Response.TransmitFile(file.FullName);
Response.End();
}
else{
Response.Write("NO FILE PRESENT");}
}
}
File will be downloaded having a wizard -Open or Save.
No comments:
Post a Comment