Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void uploadFile_Click(object sender, EventArgs e)
       {
           if(UploadImages.HasFile)
           {
               foreach(HttpPostedFile myfile in UploadImages.PostedFile)

               {
                   myfile.SaveAs(System.IO.Path.Combine(Server.MapPath("~\\uploadfile\\"),myfile.FileName));
                   listofuploadedfiles.Text += String.Format("{0}<br />", myfile.FileName);
               }
           }


asp.net....
XML
<asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />
    <asp:Button runat="server" ID="myfile" Text="Upload" OnClick="uploadFile_Click" />
    <asp:Label ID="listofuploadedfiles" runat="server" />
Posted

The error message is pretty explicit: you can't use For Each on something that isn't a collection.

The property name itself is a clue: "PostedFile" is not pluralised, implying it contains a single item, and not a collection of items. Try the PostedFiles property instead.
 
Share this answer
 
As per MSDN[^], you cannot use for each with UploadImages.PostedFile since it is not a collection modify your code to...
C#
if(UploadImages.HasFile)
{
    HttpPostedFile myfile = UploadImages.PostedFile)
    
    myfile.SaveAs(System.IO.Path.Combine(Server.MapPath("~\\uploadfile\\"),myfile.FileName));
    listofuploadedfiles.Text += String.Format("{0}<br />", myfile.FileName);
}



[Edit member="Tadit"]
Corrected formatting issues.
[/Edit]
 
Share this answer
 
v2

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