1

In my code attached below, I'm trying to upload a file via ASP.NET. I am dynamically creating the FileUpload control so that means it's not in my ViewState which (I think) means I can't use the control for uploading files unless I use the old fashioned multipat/form-data way which I don't want to do. I need to be able to allow the user to create multiple FileUpload fields and then when they click the Upload File(s) button, it loops through all the FileUpload fields and uploads them to the server.

I'm sure there's a way to do this that I'm just not thinking of - TIA!

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim fup As New FileUpload()
        fup.ID = "FileUpload1"

        PlaceHolder1.Controls.Add(fup)
    End Sub

    Protected Sub btnUploadFile_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ' HOW DO I GET THE FILE THAT WAS SELECTED IN THE DYNAMICALLY CREATE FILEUPLOAD CONTROL?
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="padding:13px">
        <asp:Button ID="btnAdd" runat="server" Text="Add FileUpload Control" OnClick="btnAdd_Click" />
        <br /><br />
        <asp:PlaceHolder ID="PlaceHolder1" runat="server" />
        <br /><br />
        <asp:Button ID="btnUploadFile" runat="server" Text="Upload File(s)" OnClick="btnUploadFile_Click" />
    </div>
    </form>
</body>
</html>

3 Answers 3

2

You can use Request.Files

It contains the uploaded files as HttpPostedFile objects.

foreach(HttpPostedFile file in Request.Files)
  file.SaveAs(...);
Sign up to request clarification or add additional context in comments.

Comments

1

here is a longer version of the above: C#

print("HttpFileCollection UploadedFiles = Request.Files;
  HttpPostedFile UserPostedFile;
  int UploadFileCount = UploadedFiles.Count;
  if (UploadFileCount >= 1)
  {
    for (int i = 0; i < UploadFileCount; ++i)
    {
      UserPostedFile = UploadedFiles[i];
      UserPostedFile.SaveAs(UserPostedFile.FileName);
    }
  }");

Comments

0

The problem is that the FileUpload control has locked off the FileName parameter from being set programmatically. The reason for this is to protect the user from some malicious script deciding that it wants to upload system files to the server instead of what the user wants.

You will not be able to use the FileUpload control in the situation you describe above, you'll want to seek out an alternative.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.