locked
Bypass FileUpload browse RRS feed

  • Question

  • User1510859543 posted

    Is there a way to trigger an event before the <input> onchange event?  I have tried onclick, onmousedown, etc. but none of them run before the file browser popup.  I want to be able to cancel the browser popup if a condition exists and just give the user a message.  Below is my aspx page markup line and js function.

    <asp:FileUpload ID="FileUpload1" runat="server" accept="image/*" onclick="return hasInfo();" onchange="uploadPhotos()" />


    function hasInfo() { var vtxt = document.getElementById('txtName'); if (vtxt === '' || vtxt === null) { alert('Name is empty. Process stopped.'); vtxt.focus(); return false; } return true; }

    Friday, March 12, 2021 6:38 PM

Answers

  • User-474980206 posted

    You don’t cancel the buttons postback

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 14, 2021 6:01 PM

All replies

  • User-821857111 posted

    You have no control over the file dialog appearance. The only thing you can do is to hide the FileUpload if the condition exists. If you explained the nature of the condition, perhaps someone can provide an example for you.

    Friday, March 12, 2021 9:09 PM
  • User1510859543 posted

    The FileUpload controls are actually doing immediate uploads using jquery $.ajax POST process. My concern is that above these controls are TextBoxes that have required identifying information that is updated on the server so I did not want to allow that until the TextBoxes were filled.

    Friday, March 12, 2021 10:55 PM
  • User-474980206 posted

    Make the file control hidden with style. Then create a button. On the button click do your validation, if valid call the click() method of the file control.

    Saturday, March 13, 2021 3:38 PM
  • User1510859543 posted

    I tried this but the browse files still popped up followed by my alert message. Below is the relevant markup and code. Note also that there will be multiple FileUpload controls on the final page.

                  <asp:TextBox ID="txtName" runat="server" Width="200"></asp:TextBox>
    
                  <asp:Button ID="BtnUpload1" runat="server" Text="Browse..." OnClientClick="return hasInfo(this);" ToolTip="Click to search" />
                  <asp:FileUpload ID="FileUpload1" runat="server" accept="image/*" onchange="uploadPhotos()" CssClass="Hide" />
    
    
            function hasInfo(obj) {
                var vtxt = document.getElementById('txtName');
                if (vtxt === '' || vtxt === null) {
                    alert('Name is empty. Process stopped.');
                    vtxt.focus();
                    return false;
                }
                var vup;
                if (obj.id === 'BtnUpload1') {
                    vup = document.getElementById('FileUpload1');
                }
                if (obj.id === 'BtnUpload2') {
                    vup = document.getElementById('FileUpload2');
                }
                vup.className = 'Show';
                obj.className = 'Hide';
                vup.click();
    
                //return true;
            }
    

    Saturday, March 13, 2021 6:51 PM
  • User-474980206 posted

    You don’t cancel the buttons postback

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 14, 2021 6:01 PM