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:
Also, I tried using Request.Files, but this just gives me the id of the FileUpload
control:
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?
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);
}
}
}