Hi
I have below code & i want to upload multiple Excel Files
Code
C#
protected void btnUploadSave_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.HasFile)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string RandomName = Utility.CurrentDateTime().ToFileTime().ToString();
string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string FolderPath = "~/Upload/ExcelUpload/";
string FilePath = Server.MapPath(FolderPath + RandomName + FileName);
FileUpload1.SaveAs(FilePath);
if (Extension.ToLower() == ".csv")
{
UploadSave(Utility.ReadCSVFileNew(FilePath));
}
else
{
UploadSave(Utility.ReadExcelFile(FilePath));
}
divUpload.Style.Add("display", "none");
btnUploadSave.Style.Add("display", "none");
if (myDataTable0.Rows.Count == 0)
{
ShowMessageWithRedirect(recno.ToString(), " Leads Uploaded Successfully", "success", "ViewOpenLeads");
}
else
{
ShowMessage(recno.ToString() + " Leads Uploaded Successfully", myDataTable0.Rows.Count + " Duplicate Leads Found", "info");
}
}
}
catch (Exception ex)
{
Utility.SaveErrorLog(ex.Message, System.IO.Path.GetFileName(Request.Path), System.Reflection.MethodBase.GetCurrentMethod().Name, Convert.ToInt32(hdfBDOID.Value));
ShowMessage("Oops...", ex.Message, "error");
}
}Thanks

Ramco RamcoRank #462 · 5.4K reputationPosted May 21, 2023, 8:12 AM EDT
Hi
Currently i am uploading single file. I want to upload multiple excel sheets at same time.
Thanks
Tuhin PaulRank #7 · 71.9K reputationPosted May 21, 2023, 10:17 AM EDT
Few suggestions::
1. Use `async` and `await` for asynchronous file upload and processing operations to improve performance and responsiveness.
protected async void btnUploadSave_Click(object sender, EventArgs e) { try { if (FileUpload1.HasFiles) { string FolderPath = "~/Upload/ExcelUpload/"; foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles) { string FileName = Path.GetFileName(uploadedFile.FileName); string RandomName = Utility.CurrentDateTime().ToFileTime().ToString(); string Extension = Path.GetExtension(uploadedFile.FileName); string FilePath = Server.MapPath(FolderPath + RandomName + FileName); uploadedFile.SaveAs(FilePath); if (Extension.ToLower() == ".csv") { await Task.Run(() => UploadSave(Utility.ReadCSVFileNew(FilePath))); } else { await Task.Run(() => UploadSave(Utility.ReadExcelFile(FilePath))); } // You can add further logic here to handle each uploaded file as needed } divUpload.Style.Add("display", "none"); btnUploadSave.Style.Add("display", "none"); if (myDataTable0.Rows.Count == 0) { ShowMessageWithRedirect(recno.ToString(), "Leads Uploaded Successfully", "success", "ViewOpenLeads"); } else { ShowMessage(recno.ToString() + " Leads Uploaded Successfully", myDataTable0.Rows.Count + " Duplicate Leads Found", "info"); } } } catch (Exception ex) { Utility.SaveErrorLog(ex.Message, System.IO.Path.GetFileName(Request.Path), System.Reflection.MethodBase.GetCurrentMethod().Name, Convert.ToInt32(hdfBDOID.Value)); ShowMessage("Oops...", ex.Message, "error"); } }2. Consider using a more descriptive and meaningful variable name instead of `FileUpload1`. For example, `fileUploadControl` or something that reflects its purpose.
3. Move the file processing logic into a separate method to improve code readability and maintainability.
private void ProcessUploadedFile(HttpPostedFile uploadedFile) { string FileName = Path.GetFileName(uploadedFile.FileName); string RandomName = Utility.CurrentDateTime().ToFileTime().ToString(); string Extension = Path.GetExtension(uploadedFile.FileName); string FilePath = Server.MapPath(FolderPath + RandomName + FileName); uploadedFile.SaveAs(FilePath); if (Extension.ToLower() == ".csv") { UploadSave(Utility.ReadCSVFileNew(FilePath)); } else { UploadSave(Utility.ReadExcelFile(FilePath)); } // You can add further logic here to handle each uploaded file as needed } protected async void btnUploadSave_Click(object sender, EventArgs e) { try { if (FileUpload1.HasFiles) { string FolderPath = "~/Upload/ExcelUpload/"; foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles) { await Task.Run(() => ProcessUploadedFile(uploadedFile)); } // Rest of the code } } catch (Exception ex) { // Exception handling code } }Ramco RamcoRank #462 · 5.4K reputationPosted May 22, 2023, 1:07 AM EDT
Hi Tuhin
Getting below error - I am getting below error - An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked @ Page Async="true" >. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.
Thanks
Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted May 21, 2023, 8:00 AM EDT
Hi ramco may i knw whats the difficulty you are facing with the above code?
Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted May 21, 2023, 8:17 AM EDT
Got it
csharp
protected void btnUploadSave_Click(object sender, EventArgs e) { try { if (FileUpload1.HasFiles) // Use HasFiles instead of HasFile to check for multiple file selection { string FolderPath = "~/Upload/ExcelUpload/"; foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles) { string FileName = Path.GetFileName(uploadedFile.FileName); string RandomName = Utility.CurrentDateTime().ToFileTime().ToString(); string Extension = Path.GetExtension(uploadedFile.FileName); string FilePath = Server.MapPath(FolderPath + RandomName + FileName); uploadedFile.SaveAs(FilePath); if (Extension.ToLower() == ".csv") { UploadSave(Utility.ReadCSVFileNew(FilePath)); } else { UploadSave(Utility.ReadExcelFile(FilePath)); } // You can add further logic here to handle each uploaded file as needed } divUpload.Style.Add("display", "none"); btnUploadSave.Style.Add("display", "none"); if (myDataTable0.Rows.Count == 0) { ShowMessageWithRedirect(recno.ToString(), "Leads Uploaded Successfully", "success", "ViewOpenLeads"); } else { ShowMessage(recno.ToString() + " Leads Uploaded Successfully", myDataTable0.Rows.Count + " Duplicate Leads Found", "info"); } } } catch (Exception ex) { Utility.SaveErrorLog(ex.Message, System.IO.Path.GetFileName(Request.Path), System.Reflection.MethodBase.GetCurrentMethod().Name, Convert.ToInt32(hdfBDOID.Value)); ShowMessage("Oops...", ex.Message, "error"); } }Hop[ew this updated code helps