locked
Values in Array RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have five FileUpload Controls & an array of 5 . I want to save the values in array of those FileUpload controls which have files in it.

    If user has uploaded file in FileUploadControl1 & FileUplaodControl4 then arr[0] & arr[1] should have values.

    If user has uploaded file in FileUploadControl1 , FileUplaodControl2 , FileUplaodControl4 then arr[0] & arr[1] & arr[2] should have values.

    Thanks

    Thursday, January 3, 2019 4:22 PM

All replies

  • User753101303 posted

    Hi again,

    I would create an array that holds each control and then would use https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.fileupload.hasfile?view=netframework-4.7.2 to see if a particular control is used ;:

    var arr=new FileUpload[]{FileUploadControl1.FileUploadControl2,FileUploadControl3,FileUploadControl4);
    foreach(var fileUpload in arr)
    {
         if (fileUpload.HasFile) continue;// next one...
         // process upload here
    }

    Another option is to use a single control (which alllows to upload files)

    Avoid to ask the same question multiple times. Just try to be as explicit as possible keeping in mind we know nothing else than whay you told us. µI understood your previous question as trying to prevent duplicate files between controls rather than just trying to test if a particular control is used.

    Thursday, January 3, 2019 4:37 PM
  • User475983607 posted

    Hi

      I have five FileUpload Controls & an array of 5 . I want to save the values in array of those FileUpload controls which have files in it.

    If user has uploaded file in FileUploadControl1 & FileUplaodControl4 then arr[0] & arr[1] should have values.

    If user has uploaded file in FileUploadControl1 , FileUplaodControl2 , FileUplaodControl4 then arr[0] & arr[1] & arr[2] should have values.

    Thanks

    See your other thread!

    https://forums.asp.net/p/2151007/6244412.aspx?Re+File+Upload

    Thursday, January 3, 2019 4:37 PM
  • User-1174608757 posted

    Hi jagjitSingh 

    According to your description,could you please tell me what values you want to save  and where does the array work, in front end  or code behind?If you want to work in front end, I suggest you to use jquery to save the file. You could judge whether the fileupload control is null or not ,then you could post the data into array as if it exists file.

    Here is my code,I hope it can help you.

    <head runat="server">
        <title></title>
        <script src="../Scripts/jquery-3.3.1.js"></script>
        <script>
            $(function () {
                $("#But1").click(function () {
                    var arry = new Array();
                    $('input[type="file"]').each(function () {
                        if (this.files.length > 0) /* judge whether controls has file*/ {
                            var filename = $(this).val().substring(12);
                            arry.push(filename);/* push data into array */
                        }
                    })
                    for (var i = 0; i <arry.length; i++) {
                        
                            alert(arry[i]+"has been chosen");
                     
    
                    }
                })
    
            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:FileUpload ID="FileUpload2" runat="server" />
                <asp:FileUpload ID="FileUpload3" runat="server" />
                <asp:FileUpload ID="FileUpload4" runat="server" />
                <asp:Button ID="Button1" runat="server" Text="Button" />
                <asp:Button ID="But1" runat="server" Text="upload" />
            </div>
        </form>
    </body>

    Best Regards

    Wei Zhang

    Friday, January 4, 2019 7:13 AM