Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I am uploading multiple files from ASP.Net web form. we have one page with multiple panels

from panel 2 i am attaching 5 files and from panel 5 i am submitting the page

i am saving the file information in sessions and assigning to FileUpload in Ispostback

ObjFileUploader1 = (FileUpload)Session["FileUpload1"];
ObjFileUploader2 = (FileUpload)Session["FileUpload2"];
ObjFileUploader3 = (FileUpload)Session["FileUpload3"];
ObjFileUploader4 = (FileUpload)Session["FileUpload4"];
ObjFileUploader5 = (FileUpload)Session["FileUpload5"];

on submit button i want to iterate through ObjFileUploader1 , ObjFileUploader2 , ObjFileUploader3,ObjFileUploader4,ObjFileUploader5

to save in DB and Server

request.files not working because of i am in panel 5

Currently i am doing like below , repetitive code

if (temp != 0 && ObjFileUploader1 != null && ObjFileUploader1.HasFile)
{
Save DB and upload file
}

if (temp != 0 && ObjFileUploader2 != null && ObjFileUploader2.HasFile)
{
Save DB and upload file
}

if (temp != 0 && ObjFileUploader3 != null && ObjFileUploader3.HasFile)
{
Save DB and upload file
}

Please help me

What I have tried:

Request.file collection not working
Request.files
Posted
Updated 14-Feb-18 22:51pm
Comments
Laiju k 14-Feb-18 23:31pm    
what error you are getting?
Ziee-M 15-Feb-18 5:05am    
Why dont you create your own list as global variable in the page and use it ?
You can save the list a viewstate instead of sessions.
Sessions are evil by the way, there is countless article about the topic.
Richard Deeming 15-Feb-18 12:04pm    
"Global variables" typically refers to static/shared fields, which are the ultimate evil in a web application. There is only a single value shared between all requests from all users, so you end up storing one user's data to another user's records.

Storing the values in ViewState is also not a good idea. Even if they would serialize, you'd end up with the user uploading the files; your code sending the content of the files back to the user in the ViewState; the user posting the content of the files back to the server in ViewState; the server sending the content of the files back to the user in the ViewState; etc.

Session is a better choice, and certainly isn't "evil". But you'll still run into problems trying to store large amounts of data in the server's memory. It would probably be best to save the files in a temporary location, using randomly-generated names; store the names in the session; and have code in the Session_End event to delete any files that haven't been processed.
Richard Deeming 15-Feb-18 12:07pm    
NB: If you're using .NET 4.5 or later, you can set the AllowMultiple[^] property on a single FileUpload control to allow the user to upload multiple files, rather than using multiple FileUpload controls.

Browser support[^] is pretty reasonable.

1 solution

I don't fully understand the question but I think you are looking to loop through your upload controls. Put them in a placeholder

<asp:PlaceHolder ID="placeUplode" runat="server">
    <asp:FileUpload ID="ObjFileUploader1" runat="server" />
    <asp:FileUpload ID="ObjFileUploader2" runat="server" />
    <asp:FileUpload ID="ObjFileUploader3" runat="server" />
    <asp:FileUpload ID="ObjFileUploader4" runat="server" />
    <asp:FileUpload ID="ObjFileUploader5" runat="server" />
</asp:PlaceHolder>


then in your code access them like this

foreach(FileUpload uploader in placeUplode.Controls.OfType<FileUpload>())
{
    System.Diagnostics.Debug.WriteLine(uploader.ID);
    if (uploader.HasFile)
    {
        // your code here
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900