Friday, 12 July 2013

Multiple File Upload and store in list box


aspx.cs

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

public partial class Default2 : System.Web.UI.Page
{
    int count = 0;
    public static ArrayList Files = new ArrayList();
    LookUp _objLookup = new LookUp();
    Properties _objProperties = new Properties();
    DataTable _objdt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            LoanBind();
        }
    }
    public void LoanBind()
    {
        _objdt = _objLookup.LoanTypes();
        ddlLoanType.DataSource = _objdt;
        ddlLoanType.DataTextField = "LoanType";
        ddlLoanType.DataValueField = "LoanTypeId";
        ddlLoanType.DataBind();
    }

   
    protected void Button1_Click(object sender, EventArgs e)
    {

        try
        {
            if (FileUpload1.HasFile)  // that the FileUpload control contains a file.
            {
                int i = FileUpload1.PostedFile.ContentLength;
                if (i > 0) // Get the size of the upload file.
                {
                    if (ListBox1.Items.Contains(new ListItem(System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)))) // Get the name of the file to upload.
                    {
                        Label1.Text = "File already in the list";
                    }
                    else
                    {
                        Files.Add(FileUpload1);
                        ListBox1.Items.Add(System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName));
                        Label1.Text = "Add another file or click Upload to save them all";
                        Label1.Visible = true;
                    }
                }
                else
                {
                    Label1.Text = "File size cannot be 0";
                }
            }
            else
            {
                Label1.Text = "Please select a file to add";
                Label1.Visible = true;
            }
        }
        catch (Exception ex)
        {

        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (ListBox1.Items.Count > 0)
        {
            if (ListBox1.SelectedIndex < 0)
            {
                Label1.Text = "Please select a file to remove";
                Label1.Visible = true;
            }
            else
            {
                Files.RemoveAt(ListBox1.SelectedIndex);
                ListBox1.Items.Remove(ListBox1.SelectedItem.Text);
                Label1.Text = "File removed";
                Label1.Visible = true;
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < ListBox1.Items.Count; i++)
        {
            _objProperties._Filename = ListBox1.Items[i].Text;
            _objProperties._LoanType = ddlLoanType.SelectedItem.Value;
            _objProperties._UserId = Convert.ToInt32(Session["userid"].ToString());
            count = _objLookup.istLoanDetails(_objProperties);
            FileUpload fileupload = (FileUpload)Files[i];
            fileupload.PostedFile.SaveAs(MapPath("~//Files//") + ListBox1.Items[i].Text);
        }
        if (count > 0)
        {
            Label1.Text = "Uploaded Successfully";
            Label1.Visible = true;
        }
    }
}


Aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Master.master" AutoEventWireup="true" CodeFile="LoanForm.aspx.cs" Inherits="Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .style1
        {
            width: 34%;
            height: 145px;
        }
        .style2
        {
            width: 484px;
        }
        .style3
        {
            width: 489px;
        }
    </style>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <center>
<div>
              <p>
                  <asp:DropDownList ID="ddlLoanType" runat="server">
                  </asp:DropDownList>
              </p>
              <p>
                  <asp:FileUpload ID="FileUpload1" runat="server" /><br />
              </p>
              <p>
                  <asp:ListBox ID="ListBox1" runat="server" Rows="5" SelectionMode="Single" Width="221px"
                      BackColor="#CCCCFF"></asp:ListBox>
                  <br />
                  <asp:Label ID="Label1" Font-Size="XX-Large" Font-Bold="true" Font-Italic="true" ForeColor="White"
                      runat="server" Text="Label" Visible="false"></asp:Label>
              </p>
              <p>
                  <asp:Button ID="Button1" runat="server" Text="Add" Width="75px" OnClick="Button1_Click" />
                  <asp:Button ID="Button2" runat="server" Text="Remove" Width="98px" OnClick="Button2_Click" />
                  <asp:Button ID="Button3" runat="server" Text="Upload" Width="98px" OnClick="Button3_Click" />
              </p>
          </div>
   </center>  
   
     <%--<asp:HyperLink ID="HyperLink1" Text="Logout" Font-Bold="true" ForeColor=White NavigateUrl="Login.aspx" runat="server"></asp:HyperLink>--%>
</asp:Content>

No comments:

Post a Comment