2

I am using 10 asp:fileUpload control to upload multiple files

Now I want to check on each upload that it has file in it or not?

For that I took an array as

FileUpload uploadarr[] = new FileUpload[10];

but how do I check that it has posted file or not using 10 controls as FileUpload1,FileUpload2,FileUpload3...FileUpload10

HttpPostedFile myFile = FileUpload1.PostedFile;

2
  • What do you need as the result, true or false for all 10 controls having a file? A list with and without files? A list of the uploaded files? Commented Sep 7, 2011 at 10:38
  • I need list of uploaded files Commented Sep 7, 2011 at 10:45

3 Answers 3

2

Rather than storing each FileUpload in an array, you could make use of the HttpRequest.Files property to recurse all files posted to the page like so:

Markup

<div><asp:FileUpload ID="FileUpload1" runat="server" /></div>
<div><asp:FileUpload ID="FileUpload2" runat="server" /></div>
<div><asp:FileUpload ID="FileUpload3" runat="server" /></div>
<asp:Button ID="UploadFilesButton" runat="server" Text="Upload Files" OnClick="UploadFilesButton_Click" />

Code

protected void UploadFilesButton_Click(object sender, EventArgs e)
{
    HttpFileCollection uploadedFiles = Request.Files;
    for (int fileIndex = 0; fileIndex < uploadedFiles.Count; fileIndex++)
    {
        HttpPostedFile uploadedFile = uploadedFiles[fileIndex];
        string fileName = System.IO.Path.GetFileName(uploadedFile.FileName);

        if (!string.IsNullOrEmpty(fileName))
        {
            //Upload file as required
            //uploadedFile.SaveAs("??");
        }
    }
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

With linq, try this:

var files = uploadarr.Where(x=>x.HasFile).Select(x=>x.PostedFile);

Comments

0

In page .cs file:

protected override void OnInit(EventArgs e)
{

    for (int i = 0; i < 10; i++)
    {
        // Panel is ASP panel
        Panel.Controls.Add(new FileUpload());
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    foreach (var p in Panel.Controls)
    {
        if (p is FileUpload)
            Response.Write(((FileUpload)p).FileName);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.