Save uploaded file count using ASP.Net AjaxFileUpload control in C# and VB.Net

Last Reply one year ago By pandeyism

Posted one year ago

Hi All ,

I am using ajaxfileupload for multiple files and i want to save the count of each files uplaod.

Suppose file 1 upload count =1

file 2 Count =2 upto 5 files i have set limit.

I need to save the file count in every excel. Suppose one excel upload

In database there will be FileCount Column and for excel 1, 1 should saved.

<asp:AjaxFileUpload ID="AjaxFileUploadAdvanceAdjusted" runat="server" Width="850px" AllowedFileTypes="xlsx,xls" MaxFileSize="10000" MaximumNumberOfFiles="5" OnUploadComplete="AjaxFileUpload_UploadComplete" />

 

protected void AjaxFileUpload1_UploadComplete(object sender, AjaxFileUploadEventArgs e)
{
    dt2 = new DataTable();
    try
    {
        string filename = Path.GetFileName(e.FileName);
        string FolderPath = "~/MyFiles/Files/";
        string filepath = Server.MapPath(FolderPath + filename);
        AjaxFileUploadOutward.SaveAs(filepath);

        //To set dafault coloumn in datatable and save in datatabase.
        dtExcelData.Columns.Add("Cell").DefaultValue.ToString();
        dtExcelData.Columns.Add("FileCount1").DefaultValue.ToString();

        //Foreach to add in every row in datatable.
        foreach (DataRow row in dtExcelData.Rows)
        {
            row["Cell"] = Session["ClientName"].ToString(); 
            row["FileCount1"] = ???
        }
        dtExcelData.Columns["Cell"].SetOrdinal(0);
        dtExcelData.Columns["FileCount1"].SetOrdinal(1);

        //Bulkinsert method to insert data in sql.
        InsDataInSql("File1", "abcFile1");
    }
}

Please help.

You are viewing reply posted by: pandeyism one year ago.
Posted one year ago Modified on one year ago

Hi BugHunter,

You need use UploadCompleteAll event which will give count of uploaded file.

Refer below code.

Namespaces

C#

using System.IO;
using AjaxControlToolkit;

VB.Net

Imports System.IO
Imports AjaxControlToolkit

Code

C#

static int i = 0;
protected void OnUploadComplete(object sender, AjaxFileUploadEventArgs e)
{
    string fileName = Path.GetFileName(e.FileName);
    AjaxFileUpload11.SaveAs(Server.MapPath("~/Uploads/" + fileName));
    i = i + 1;
    dtExcelData.Columns.Add("Cell").DefaultValue.ToString();
    dtExcelData.Columns.Add("FileCount1").DefaultValue.ToString();

    //Foreach to add in every row in datatable.
    foreach (DataRow row in dtExcelData.Rows)
    {
        row["Cell"] = Session["ClientName"].ToString();
        row["FileCount1"] = i; // Uploaded file count.
    }
    dtExcelData.Columns["Cell"].SetOrdinal(0);
    dtExcelData.Columns["FileCount1"].SetOrdinal(1);

    //Bulkinsert method to insert data in sql.
    InsDataInSql("File1", "abcFile1");
}

VB.Net

Shared i As Integer = 0
Protected Sub OnUploadComplete(ByVal sender As Object, ByVal e As AjaxFileUploadEventArgs)
    Dim fileName As String = Path.GetFileName(e.FileName)
    AjaxFileUpload11.SaveAs(Server.MapPath("~/Uploads/" & fileName))
    i = i + 1
    dtExcelData.Columns.Add("Cell").DefaultValue.ToString()
    dtExcelData.Columns.Add("FileCount1").DefaultValue.ToString()
    For Each row As DataRow In dtExcelData.Rows
        row("Cell") = Session("ClientName").ToString()
        row("FileCount1") = i
    Next
    dtExcelData.Columns("Cell").SetOrdinal(0)
    dtExcelData.Columns("FileCount1").SetOrdinal(1)
    InsDataInSql("File1", "abcFile1")
End Sub