简体   繁体   中英

Select multiple files at a time using FileUpload control in asp.net

Here's my code

protected void Page_Load(object sender, EventArgs e)
{
    tbusrcompdate.Attributes.Add("readonly", "readonly");
    if (!IsPostBack)
    {
        //using session state
        if (Session["userid"] == null)
        {
            Session["userid"] = "Compliance_Tracker";
            lblsessionID.Text = Session["userid"].ToString();
        }
        else
        {
            lblsessionID.Text = Session["userid"].ToString();
        }

        //populating ddlTaskId
        string query = "select * from Compliance_Tracker.dbo.tasklistManager where STATUS='1';";
        string columnname = "TASK ID";
        string datavaluefield = "TASK ID";
        obj7.PopulateCombo(ddlTaskID, query, columnname, datavaluefield);
        //default values in labels
        string query1 = "select top 1 [DESC] from Compliance_Tracker.dbo.tasklistManager where STATUS = '1';";
        lblDescOutput.Text = obj7.ExecuteScalar(query1).ToString();
        string query2 = "select top 1 FREQUENCY from Compliance_Tracker.dbo.tasklistManager where STATUS = '1';";
        lblFrequencyOutput.Text = obj7.ExecuteScalar(query2).ToString();

    }
}
 protected void ddlTaskID_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectedtext = ddlTaskID.SelectedValue.ToString();
    string query = "select [DESC] from Compliance_Tracker.dbo.tasklistManager where Compliance_Tracker.dbo.tasklistManager.[TASK ID] ='" + selectedtext + "';";
    lblDescOutput.Text = obj7.ExecuteScalar(query).ToString();
}
protected void btnAdd_Click(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    if (fuDocpath.HasFiles)
    {
        try
        {
            DateTime now = DateTime.Now;
            lbldateStamp.Text = now.ToString("mm_dd_yyyy_hh_mm_ss");
            string basicPath = Server.MapPath("~/Uploaded_Files/");
            string foldername = lblsessionID.Text + "_" + lbldateStamp.Text;
            string folderpath = (basicPath + foldername + "/");
            Directory.CreateDirectory(folderpath);
            foreach (HttpPostedFile file in fuDocpath.PostedFiles)
            {
                string filename = Path.GetFileName(fuDocpath.FileName);
                string folderpath1 = folderpath + "/";
                fuDocpath.SaveAs(folderpath1 + filename);
                lblName.Text =  lblName.Text+"|" + filename;
                lblerror.Text = string.Empty;
            }
        }
        catch (Exception ex)
        {
            lblerror.Text = "File couldn't be uploaded." + ex.Message;
            lblName.Text = string.Empty;
        }
    }
}

Now , using the above code I am only able to do single selection even though I can upload multiple files totally. So I need to know ,if there's a way to select multiple files at a time using fileupload tool OR will I have to use any other tool? Here's my aspx code

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder3" Runat="Server">
<form id="Form1" runat="server" style="border-style: none; border-width: inherit; border-color: #008000; background-color:#33CC33; height:588px; width:669px; background-image: url('new.jpg'); background-repeat: no-repeat;" method="post" enctype="multipart/form-data">
    <h1 style="height: 34px">
        TRANSACTION MANAGER TABLE
    </h1>
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
     <asp:Label ID="lblsessionID" runat="server" Text=""></asp:Label>
    <asp:Label ID="lbldateStamp" runat="server" Text=""></asp:Label>
     <table id="table1" style="border-style: none; height:188px; width:549px; margin-left:30px; border-collapse: collapse; margin-top: 0px;">
        <tr>
            <td style="width: 137px; height:30px;"><asp:Label ID="lblTaskID" runat="server" Text="TASK ID" Width="70px"></asp:Label></td>
            <td style="height:30px"><asp:DropDownList ID="ddlTaskID" runat="server" Height="20px" style="margin-left: 50px" Width="126px" OnSelectedIndexChanged="ddlTaskID_SelectedIndexChanged" AutoPostBack="true" ></asp:DropDownList></td>
        </tr>
        <tr>
            <td style="width:137px; height:30px;"><asp:Label ID="lblDesc" runat="server" Text="DESC" Width="70px"></asp:Label></td>
            <td style ="height:30px"><asp:Label ID="lblDescOutput" runat="server" style="margin-left:50px" Width="126px" Height="20px"></asp:Label></td>
        </tr>
        <tr>
            <td style="width: 137px; height:30px;"><asp:Label ID="lblFrequency" runat="server" Text="FREQUENCY" Width="132px"></asp:Label></td>
            <td style="height:30px"><asp:Label ID="lblFrequencyOutput" runat="server" style="margin-left:50px" Width="126px" Height="20px"></asp:Label></td>
        </tr>
        <tr>
            <td style="width: 137px; height:30px;"><asp:Label ID="lblDocpath" runat="server" Text="DOC PATH" Width="107px"></asp:Label></td>
            <td style="height:30px">

                <asp:FileUpload ID="fuDocpath" runat="server" AllowMultiple="true" />
                <asp:Button ID="Button1" runat="server" Text ="Upload File" OnClick="Button1_Click" />
                <asp:Label ID="lblerror" runat="server" Text="" style="margin-left: 50px"></asp:Label>
    <asp:Label ID="lblName" runat="server" Text=""></asp:Label>
            </td>
        </tr>
        <tr>
            <td style="width: 137px; height:30px;"><asp:Label ID="lblusrcompdate" runat="server" Text="USER COMPLETE DATE" Width="147px"></asp:Label></td>
            <td style="height:30px"><asp:TextBox ID="tbusrcompdate" runat="server"></asp:TextBox>

                <asp:ImageButton ID ="imgbtncalender" runat="server" ImageUrl="~/home-calendar-button.jpg" Height="17px" style="margin-left: 16px" Width="16px" />
                <asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="tbusrcompdate" Format="dd/MM/yyyy" PopupButtonID="imgbtncalender"></asp:CalendarExtender>
            </td>
        </tr>
    </table><br />
    <asp:Button ID="btnAdd" runat="server" Text="ADD" Height="27px" Width="80px" style="margin-left:235px" OnClick="btnAdd_Click"/>
</form>

The actual problem was with IE and after updation Iam able to select multiple files now. Another problem was with my code actually only one file was getting selected because I didn't get the file name properly. Well , it was with the code actually that in the part where I declared the filename string

foreach (HttpPostedFile files in fuDocpath.PostedFiles)
            {
                string filename = Path.GetFileName(fuDocpath.FileName);
                string folderpath1 = folderpath + "/";
                fuDocpath.SaveAs(folderpath1 + filename);
                lblName.Text =  lblName.Text+"|" + filename;
                lblerror.Text = string.Empty;
            }

Now, here the main problem was I wasn't getting the proper filename . SO , the mistake was

string filename = Path.GetFileName(files.Filename)

instead of fuDocpath.filename. Silly mistake fml!!!

This is not exactly the answer for your specific code. But it is an alternative to upload multiple files.

For uploading multiple files, I don't do all this stuff, but there is something called as asp:AjaxFileUpload control for this

<asp:AjaxFileUpload ID="AjaxFileUpload1" ToolTip="Uplaod File" OnUploadComplete="AjaxFileUpload1_UploadComplete" runat="server" />

Code Behind :

protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
    try
    {
        string str = System.IO.Path.GetFileName(e.FileName);
        string fullpath = Path.GetFullPath(str);

        string str1 = "Your insert Query";
        con.Open();

        cmd = new SqlCommand(str1, con);

        // your command parameters to add       
        cmd.Parameters.AddWithValue("@Image_Caption", str);
        cmd.Parameters.AddWithValue("@Image_Path", "Images/" + str);
        cmd.Parameters.AddWithValue("@Image_Name", str);

        cmd.ExecuteNonQuery();
        con.Close();

        string filePath = Server.MapPath("~/Images/") + System.IO.Path.GetFileName(e.FileName);
        AjaxFileUpload1.SaveAs(filePath);
    }
    catch (Exception eep)
    {
        throw;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM