0
votes

I have this code but is not compatible with 4.0, Thanks

.aspx

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

.cs

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);
           } 
        }
    } 

I have this errors:

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

Error 2 '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?)

1
This feature (for server-side WebControls) is new to 4.5. - SLaks
It will only work with 4.5. Multi-file uploads are a brand new feature of HTML. .Net 4.0 can't emit the HTML necessary to deal with them. In 4.5, the upload control is able to emit the HTML 5 version of the file upload that allows multiples as well as handle the multiple file streams. If you can't upgrade to 4.5 you will need to look for a third-party upload control that can do with in 4.0. - Mark Fitzpatrick

1 Answers

0
votes

For .NET 4.0 I found this solution without changing the project version:

 var type  = files.GetType();
 var prop  = type.GetProperty("PostedFiles");
 var files = prop?.GetValue(this.files, null) as ICollection<HttpPostedFile>;