Thursday, 27 September 2012

how can we upload and download file in asp.net

Code of upload/insert data in database:- 
 
protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
        SqlCommand cmd = new SqlCommand("application", con);
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;

        if (filephoto.HasFile && filedoc1.HasFile && filedoc2.HasFile && filedoc3.HasFile)
        {
            string fileExt = System.IO.Path.GetExtension(filephoto.FileName);


            string fileExt1 = System.IO.Path.GetExtension(filedoc1.FileName);
            string fileExt2 = System.IO.Path.GetExtension(filedoc2.FileName);
            string fileExt3 = System.IO.Path.GetExtension(filedoc3.FileName);


            //if (FileUpload1.HasFile)
            //{
            //    FileUpload1.SaveAs(Server.MapPath("~/StockImages\\" + FileUpload1.FileName));
            //    _obj.ImagePath = FileUpload1.PostedFile.FileName;

            //}
            //else
            //{
            //    _obj.ImagePath = "";

            //}

            s = Server.MapPath("~/img\\" + filephoto.FileName);
            //s = s + "/" + FileUpload1.PostedFile.FileName;
            filephoto.SaveAs(s);

            s = Server.MapPath("~/documents\\" + filedoc1.FileName);
            filedoc1.SaveAs(s);

            s = Server.MapPath("~/documents\\" + filedoc2.FileName);
            filedoc2.SaveAs(s);

            s = Server.MapPath("~/documents\\" + filedoc3.FileName);
            filedoc3.SaveAs(s);


        }
        cmd.Parameters.AddWithValue("@fname", txtFname.Text);
        cmd.Parameters.AddWithValue("@lanme", txtLname.Text);
        cmd.Parameters.AddWithValue("@address", txtAddress.Text);
        cmd.Parameters.AddWithValue("@state", txtState.Text);
        cmd.Parameters.AddWithValue("@city", txtCity.Text);
        cmd.Parameters.AddWithValue("@sex", ddSex.SelectedItem.Text);
        cmd.Parameters.AddWithValue("@mother_tounge", txtMothertounge.Text);
        cmd.Parameters.AddWithValue("@place_birth", txtBirth.Text);
        cmd.Parameters.AddWithValue("@fathername", txtFather.Text);
        cmd.Parameters.AddWithValue("@nationality", txtNationality.Text);
        cmd.Parameters.AddWithValue("@postal_address", txtPostal.Text);
        cmd.Parameters.AddWithValue("@residence_no", txtResidence.Text);
        cmd.Parameters.AddWithValue("@mobile", txtMobile.Text);
        cmd.Parameters.AddWithValue("@category", ddCategory.SelectedItem.Text);
        cmd.Parameters.AddWithValue("@email", txtEmail.Text);
        cmd.Parameters.AddWithValue("@admission_cycle", ddAdmissioncycle.SelectedItem.Text);
        cmd.Parameters.AddWithValue("@university_name", txtUniversityname.Text);
        cmd.Parameters.AddWithValue("@document2", "~/documents/" + filedoc2.PostedFile.FileName);
        cmd.Parameters.AddWithValue("@photo", "~/img/"+filephoto.PostedFile.FileName );
        cmd.Parameters.AddWithValue("@document3", "~/documents/" + filedoc3.PostedFile.FileName);
        cmd.Parameters.AddWithValue("@document1", "~/documents/"+filedoc1.PostedFile.FileName);
        cmd.Parameters.AddWithValue("@qualification", txtQualification.Text);
        cmd.Parameters.AddWithValue("@studycentercode", txtStudycenter.Text);
        cmd.Parameters.AddWithValue("@date", txtDate.Text );
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        Page.ClientScript.RegisterStartupScript(this.GetType(), "ErrorAlert", "alert('Record has been inserted!!!');", true);
         }




Code of Data/File Download :-

  protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
         string fileName = "";
        string path = "";
        if (e.CommandName == "pic")
        {
            path = Server.MapPath(e.CommandArgument.ToString());
            fileName=e.CommandArgument.ToString();
            //fileName = fileName.Substring(fileName.LastIndexOf('/'), fileName.Length - 1);
            FileInfo b = new FileInfo(path);
            fileName = b.Extension;
            Response.ClearContent();
            Response.ClearHeaders();

            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment;filename=a" + fileName);
            Response.TransmitFile(path);
            Response.End();
   
        }
        else if (e.CommandName == "DownloadDoc")
        {
            path = Server.MapPath(e.CommandArgument.ToString());
            fileName = e.CommandArgument.ToString();
            FileInfo a = new FileInfo(path);
            fileName =a.Extension;
            Response.ClearContent();
            Response.ClearHeaders();

            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment;filename=a" + fileName);
            Response.TransmitFile(path);
            Response.End();
   

        }

Code of Manual bind grid:-





   <asp:gridview ID="Gridview1" runat="server"
            EnableModelValidation="True" Font-Size="Small" AllowPaging="True" PageSize="5"
                    AllowSorting="True" onrowcommand="Gridview1_RowCommand"
          AutoGenerateColumns="False" onrowdeleting="Gridview1_RowDeleting"
          onselectedindexchanged="Gridview1_SelectedIndexChanged"
          CssClass="mGrid" onrowcreated="Gridview1_RowCreated" BackColor="WhiteSmoke" >
            <RowStyle HorizontalAlign="Left"  />
    <Columns>
    <asp:TemplateField HeaderText="Id" HeaderStyle-HorizontalAlign="Center" >
    <ItemTemplate>
        <asp:TextBox ID="txtId" runat="server" Text='<%# Eval("application_id") %>'></asp:TextBox></ItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Fname" HeaderStyle-HorizontalAlign="Center">
    <ItemTemplate><asp:TextBox ID="txtFname" runat="server" Text='<%# Eval("fname") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>

  
    <asp:TemplateField HeaderText="Lname" HeaderStyle-HorizontalAlign="Center" >
    <ItemTemplate> <asp:TextBox ID="txtLname" runat="server" Text='<%# Eval("lname") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
       </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>
   
    <asp:TemplateField HeaderText="Address" HeaderStyle-HorizontalAlign="Center">
    <ItemTemplate><asp:TextBox ID="txtAddress" runat="server" Text='<%# Eval("address") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>
   
    <asp:TemplateField HeaderText="State" HeaderStyle-HorizontalAlign="Center">
    <ItemTemplate>
    <asp:TextBox ID="txtState" runat="server" Text='<%# Eval("state") %>'></asp:TextBox>
    </ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>
   
    <asp:TemplateField HeaderText="City" HeaderStyle-HorizontalAlign="Center">
    <ItemTemplate><asp:TextBox ID="txtCity" runat="server" Text='<%# Eval("city") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>
   
    <asp:TemplateField HeaderText="Sex" HeaderStyle-HorizontalAlign="Center">
    <ItemTemplate><asp:TextBox ID="txtSex" runat="server" Text='<%# Eval("sex") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>
   
    <asp:TemplateField HeaderText="Mother Name" HeaderStyle-HorizontalAlign="Center">
    <ItemTemplate><asp:TextBox ID="txtMotherTounge" runat="server" Text='<%# Eval("mother_tounge") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>
   
    <asp:TemplateField HeaderText="Place Birth" HeaderStyle-HorizontalAlign="Center">
    <ItemTemplate><asp:TextBox ID="txtPlaceBirth" runat="server" Text='<%# Eval("place_birth") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>
   
    <asp:TemplateField HeaderText="Father Name" HeaderStyle-HorizontalAlign="Center">
    <ItemTemplate><asp:TextBox ID="txtFatherName" runat="server" Text='<%# Eval("father_name") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>

        <asp:TemplateField HeaderText="Nationality" HeaderStyle-HorizontalAlign="Center">
        <ItemTemplate><asp:TextBox ID="txtNationality" runat="server" Text='<%# Eval("nationality") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField >
        <asp:TemplateField HeaderText="Postal Address" HeaderStyle-HorizontalAlign="Center">
        <ItemTemplate><asp:TextBox ID="txtPosatalAddress" runat="server" Text='<%# Eval("postal_address") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>
        <asp:TemplateField HeaderText="Residence No" HeaderStyle-HorizontalAlign="Center">
        <ItemTemplate><asp:TextBox ID="txtResidence" runat="server" Text='<%# Eval("residence_no") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>
        <asp:TemplateField HeaderText="Mobile No" HeaderStyle-HorizontalAlign="Center">
        <ItemTemplate> <asp:TextBox ID="txtMobile" runat="server" Text='<%# Eval("mobile_no") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
       </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>
        <asp:TemplateField HeaderText="Category" HeaderStyle-HorizontalAlign="Center">
        <ItemTemplate><asp:TextBox ID="txtCategory" runat="server" Text='<%# Eval("category") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>
        <asp:TemplateField HeaderText="Email" HeaderStyle-HorizontalAlign="Center">
        <ItemTemplate><asp:TextBox ID="txtEmail" runat="server" Text='<%# Eval("email") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>
        <asp:TemplateField HeaderText="Admission Cycle" HeaderStyle-HorizontalAlign="Center">
        <ItemTemplate><asp:TextBox ID="txtAdmissionCycle" runat="server" Text='<%# Eval("admission_cycle") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>

           <asp:TemplateField HeaderText="University Name" HeaderStyle-HorizontalAlign="Center">
           <ItemTemplate><asp:TextBox ID="txtUniversityName" runat="server" Text='<%# Eval("university_name") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>

           <asp:TemplateField HeaderText="Document 1" HeaderStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:Button ID="btnDocument2"  CommandName="DownloadDoc" runat="server" CommandArgument='<%# Eval("document2") %>' Text="Download" /> </ItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>


           <asp:TemplateField HeaderText="Photo" HeaderStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:Button ID="btnPhoto" runat="server" CommandName="pic" Text="Download" CommandArgument='<%# Eval("photo") %>' />
    </ItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>

           <asp:TemplateField HeaderText="Document2" HeaderStyle-HorizontalAlign="Center">
    <ItemTemplate>
         <asp:Button ID="btnDocument3"  CommandName="DownloadDoc" runat="server" CommandArgument='<%# Eval("doucment3") %>' Text="Download" /></ItemTemplate>

  

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>

           <asp:TemplateField HeaderText="Document3" HeaderStyle-HorizontalAlign="Center">
 <ItemTemplate>
          <asp:Button ID="btnDocument1"  CommandName="DownloadDoc" runat="server" CommandArgument='<%# Eval("document1") %>' Text="Download" /></ItemTemplate>

 

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>

         <asp:TemplateField HeaderText="Qualification" HeaderStyle-HorizontalAlign="Center">
         <ItemTemplate><asp:TextBox ID="txtQualification" runat="server" Text='<%# Eval("qualification") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>

         <asp:TemplateField HeaderText="Study Center Code" HeaderStyle-HorizontalAlign="Center">
         <ItemTemplate><asp:TextBox ID="txtStudyCenterCode" runat="server" Text='<%# Eval("studycentercode") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>

       <asp:TemplateField HeaderText="Date" HeaderStyle-HorizontalAlign="Center">
       <ItemTemplate><asp:TextBox ID="txtDate" runat="server" Text='<%# Eval("date") %>'></asp:TextBox></ItemTemplate>
    <EditItemTemplate>
        </EditItemTemplate>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    </asp:TemplateField>
        <asp:CommandField ShowDeleteButton="True" />
    </Columns>
   
                   <HeaderStyle HorizontalAlign="Left" CssClass="grid_head"></HeaderStyle>

                  <AlternatingRowStyle CssClass="grid1" />
    </asp:gridview>



No comments:

Post a Comment