Wednesday, February 18, 2015

upload and download a file to grid view

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public partial class upload_download : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=ADMIN-345130E68\\SQLEXPRESS;Initial Catalog=ramya;Integrated Security=True");
    string fname, fpath;

    protected void Page_Load(object sender, EventArgs e)
    {
        {
            if (!IsPostBack)
            {

            }
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string File = (sender as LinkButton).CommandArgument;
        Response.ContentType = ContentType;
        Response.AddHeader("Content-Disposition", "attachment;Filename=" + Path.GetFileName(File));
        Response.TransmitFile(File);
        Response.End();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        fname = FileUpload1.PostedFile.FileName;
        fpath = Server.MapPath("~/files/" + FileUpload1.PostedFile.FileName);
        FileUpload1.SaveAs(fpath);
        SqlCommand cmd = new SqlCommand("insert into upload values(@p1,@p2)", con);
        cmd.Parameters.AddWithValue("@p1",fname);
        cmd.Parameters.AddWithValue("@p2",fpath);
        con.Open();
        int i = cmd.ExecuteNonQuery();
        if (i == 1)
        {
            Response.Write("<script>alert('fileuploaded')</script>");
            Label1.Text = "fileuploaded sucessfully";
        }
        else
            Response.Write("<script>alert('error')</script>");
    }
}
 
  

No comments:

Post a Comment