8

I am trying to upload multiple files on an aspx page using one FileUpload control. I've set the control to allow for multiple files:

<asp:FileUpload ID="fuAttach" Multiple="Multiple" runat="server" Visible="False" />

Now, on the click of a button I want to take the data of each of these files and save it to a database (using a REST service, but that's not important right now). According to Visual Studio, I can access the PostedFile property, but not the PostedFiles property of the FileUpload control.

'System.Web.UI.WebControls.FileUpload' does not contain a definition for 'PostedFiles' and no extension method 'PostedFiles' accepting a first argument of type 'System.Web.UI.WebControls.FileUpload' could be found (are you missing a using directive or an assembly reference?)

When debugging, however the PostedFiles property is visible and contains all of my files:

Posted Files

Also, I tried using Request.Files, but this just gives me the id of the FileUpload control:

Request.Files

Also, looking at the FileUpload control, there is no PostedFiles:

public class FileUpload : WebControl
{
    public FileUpload();

    public byte[] FileBytes { get; }
    public Stream FileContent { get; }
    public string FileName { get; }
    public bool HasFile { get; }
    public HttpPostedFile PostedFile { get; }

    protected override void AddAttributesToRender(HtmlTextWriter writer);
    protected internal override void OnPreRender(EventArgs e);
    protected internal override void Render(HtmlTextWriter writer);
    public void SaveAs(string filename);
}

Am I missing something here?

Carel
  • 2,063
  • 8
  • 39
  • 65
  • Starting point for other links: http://stackoverflow.com/questions/17441925/how-to-choose-multiple-files-using-file-upload-control – Adriano Repetti May 05 '14 at 11:36

3 Answers3

13

Go to "Application" tab in the project "Properties" and change the "Target Framework" to 4.5.

  • This worked perfect. I suspected there was some kind of version issue. Thanks. – glitzsfa Jan 06 '16 at 04:20
  • I've been searching for an answer to this for days and changing the target framework to 4.5 was it. Thank you!!! – Andy Jan 19 '16 at 20:43
  • Mine was already set to 4.5.2, but when I set it to 4.6.1 and then back to 4.5.2 it fixed it and my project now builds, so strange... – David Rogers Feb 08 '17 at 18:49
  • with 4.0 visual studio marks "PostedFiles" as wrong, but is working...with 4.5 all rights.. – elle0087 Oct 13 '21 at 12:53
6

I probably come too late, but since I am having the same problem. I decide to post my answer here for any future answer seekers. I have to use a walk-around to tackle this problem.

dynamic fileUploadControl = fileUpload1;
foreach(var file in fileUploadControl.PostedFiles)
{//do things here}

converting your fileUpload userControl to a dynamic object will allow you to bypass the compile time error checking.

user1605909
  • 106
  • 1
  • 4
2

It should be something like this :

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

html code will be like this :

<div>
    <asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />
    <asp:Button runat="server" ID="uploadedFile" Text="Upload" OnClick="uploadFile_Click" />
    <asp:Label ID="listofuploadedfiles" runat="server" />
</div>

code behind for the upload button :

protected void uploadFile_Click(object sender, EventArgs e)
{
   if (UploadImages.HasFiles)
   {
       foreach (HttpPostedFile uploadedFile in UploadImages.PostedFiles)
       {
           uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"),
           uploadedFile.FileName)); listofuploadedfiles.Text += String.Format("{0}<br />", uploadedFile.FileName);
       }
   }
} 
Krunal Patil
  • 3,666
  • 5
  • 22
  • 28