Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileUpload Contol not showing posted files

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?

like image 736
Carel Avatar asked May 05 '14 11:05

Carel


3 Answers

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

like image 171
Salem Jamal-Uddeen Avatar answered Sep 22 '22 07:09

Salem Jamal-Uddeen


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.

like image 20
user1605909 Avatar answered Sep 18 '22 07:09

user1605909


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);
       }
   }
} 
like image 45
Krunal Patil Avatar answered Sep 20 '22 07:09

Krunal Patil