FileUpload Contol not showing posted files
Asked Answered
C

3

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?

Crump answered 5/5, 2014 at 11:29 Comment(1)
Starting point for other links: #17442425Gumbotil
F
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);
       }
   }
} 
Fiord answered 5/5, 2014 at 11:38 Comment(0)
C
13

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

Correspondence answered 2/8, 2015 at 9:1 Comment(4)
This worked perfect. I suspected there was some kind of version issue. Thanks.Eldoneldora
I've been searching for an answer to this for days and changing the target framework to 4.5 was it. Thank you!!!Footplate
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...Guarantor
with 4.0 visual studio marks "PostedFiles" as wrong, but is working...with 4.5 all rights..Bargello
I
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.

Irtysh answered 29/6, 2015 at 9:4 Comment(0)
F
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);
       }
   }
} 
Fiord answered 5/5, 2014 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.