1

I am working on a simple code to upload multiple files using single upload button (AllowMultiple="true"), and I am trying to add all the uploaded files to list, but the problem is only the first file is added without the other files.

asp.net

<asp:FileUpload runat="server" ID="file1" AllowMultiple="true" />

c#

PdfReader pdfReader1 = new PdfReader(file1.PostedFile.InputStream);
List<PdfReader> readerList = new List<PdfReader>();
readerList.Add(pdfReader1);
3
  • That's because you have added one reader to you reader list. You probably want to read files with reader. Commented Mar 7, 2017 at 12:37
  • Maybe this will help: stackoverflow.com/questions/17441925/… Commented Mar 7, 2017 at 12:38
  • First, AllowMultiple="true" is a .Net 4.5 property. Are you sure to have it ? Below 4.5 it was something like Multiple="Multiple" (check on the MSDN). Second : you have to use the file1.PostedFiles collection to retrieve all files. Commented Mar 7, 2017 at 12:38

1 Answer 1

3

With PostedFile you get only one item, use PostedFiles instead:

List<PdfReader> readerList = new List<PdfReader>();
readerList.AddRange(file1.PostedFiles.Select(f=>new PdfReader(f.InputStream)))
Sign up to request clarification or add additional context in comments.

1 Comment

OP: Make sure to close/dispose the readers after you are done with them.

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.