1

This is my first time asking questions on StackOverflow, and I will be as much specific as I can.

When I opened the website with my Chrome, Firefox and IE, the submit button 'upgrade' works fine after I selected a file to upload/upgrade to the server (my router).

But when I'm trying it on my C# Winform project using the "WebBrowser" component, the submit button doesn't response at all (after I selected the file) whether I'm manually clicking the button or writing C# code to trigger the onClick event.

Does anyone have a clue? Any help would really be appreciated, thanks!

P.S. I'm sorry I can't provide to much picture about the webpage because I'm currently working under a company.

This is the what the webpage looks like enter image description here

The html code of the webpage is showed below:

<table>
    <td>
        <label class="subtt2" style="margin-bottom:8px;font-size:14px">
            Software Image:
        </label>
    </td>
    <td>
        <div class="file-box">
            <input id="firmware" name="fupgrade.bin" class="file" 
                   onchange="getfileName()" type="file" style="cursor:pointer">
            <input id="filename" class="txt" type="text">
            <input class="btnWtn" value="Browse" type="button">
            <label class="error" id="lupgradeforbidden" style="display: none"></label>
        </div>
    </td>
</table>
<div class="buttonStyle" align="left">
    <input name="UPLOAD_ACTION" id="UPLOAD_ACTION" value="Upgrade"
           onclick="upgradeRouter()" type="button">
</div>

The upgradeRouter() is as follows:

    function upgradeRouter()
{
    var FIRMWARE = document.getElementById( "firmware" );
    var UPLOAD_ACTION = document.getElementById( "upload_action" ); 

    if( FIRMWARE.value == "" )
        alert( "Please select firmware file" );
    else
    {             
        fileSize = FIRMWARE.files.item(0).size;

        if(fileSize > 0)
        {
            if( uploading == 0 )
            {              
               document.forms["myForm"].submit();
               setTimeout("firmware_update_query_request();", POLLING_FW_STATUS_SECONDS*1000);
               document.getElementById('upgradeFrame').style.display = 'none';  
               document.getElementById('upgradeModalBox').style.display = 'block';             
               UPLOAD_ACTION.value = "Cancel";
               uploading = 1;
            }
            else
            {
                uploading = 0;
                UPLOAD_ACTION.value = "Update";
                myForm.reset();
            }
        }
    }

    return false;   
}
MikeRays
  • 23
  • 6
  • You can use Ctrl + K to format code block easily. – Circle Hsiao Dec 25 '17 at 02:29
  • 1
    Thank you for the advice , I'm also a Taiwanese btw :) – MikeRays Dec 25 '17 at 02:31
  • Can you show the code for your upgradeRouter() function? – Josh Withee Dec 25 '17 at 02:32
  • @Marathon55 I've just updated the upgradeRouter() to my question. – MikeRays Dec 25 '17 at 02:40
  • you say the button click is not doing anything, how have you verified that? i would put an alert at the very beginning of your function to determine that. if you get alert, move it down one line at a time to see if something is breaking - and where. – Heriberto Lugo Dec 25 '17 at 03:48
  • setTimeout("firmware_update_query_request() i think should be setTimeout("firmware_update_query_request --- just function name, without parens – Heriberto Lugo Dec 25 '17 at 03:52
  • if i remember correctly, the webbrowser control is same engine as an older internet explorer. what i mean to say by that is that its not always very forgiving and wont always do what you think it would. there is no way to change its engine. you would have to use a different browser like chromium or gecko. i try to always use chromium now. – Heriberto Lugo Dec 25 '17 at 06:07
  • ahh.. i think i found ur answer.. it might be because there is no form tag. since webbrowser control is older ie version - it does not like that. try – Heriberto Lugo Dec 25 '17 at 06:13
  • There exists form tag in the HTML which I didn't post it up here . Or does @HeribertoLugo mean that the WebBrowser doesn't support form tag? I've tried to change the button tag into
    but it seems not working neither. (I'm still not really familiar with HTML, so please correct me if I'm wrong, THX!) Thank you for looking up references for me, I'm really deeply appreciated.
    – MikeRays Dec 25 '17 at 06:48
  • ahhh.. i didnt know there was a form tag. seems input type=button is supposed to be in form tag only. so it would work in modern browsers, but maybe not in older ie browser. webbrowser control does support form tag. – Heriberto Lugo Dec 25 '17 at 07:26
  • did you try the alerts? to see if there is a crash somewhere? – Heriberto Lugo Dec 25 '17 at 07:28
  • I've just used my computer's IE (open the IE and pressed F12) to simulate IE 7 and tested the HTML, it works pretty fine after I selected the file to upload. Maybe this excludes the problem of the WebBrowser of which browser version – MikeRays Dec 25 '17 at 07:58
  • Sorry for the former information I gave. I just discovered that it crashes on fileSize = FIRMWARE.files.item(0).size; – MikeRays Dec 25 '17 at 08:13
  • If I set the fileSize beforehand manually (EX: fileSize = 456345), everything turns out to work fine. – MikeRays Dec 25 '17 at 08:28

1 Answers1

0

Since fileSize = FIRMWARE.files.item(0).size; crash (which leads to clicking the upgrade button with no repsonse), the work around solution to my problem is just to inject script into the web page and execute my version of upgradeRouter() again.

Which I set up the value of fileSize manually beforehand.

The injection code is shown below

 public void InjectScript()
    {
        HtmlElement headElement = webBrowser1.Document.GetElementsByTagName("head")[0];
        HtmlElement scriptElement = webBrowser1.Document.CreateElement("script");
        IHTMLScriptElement element = (IHTMLScriptElement)scriptElement.DomElement;
                   element.text = "function upgradeRouter_M()" +
                               "{ " +
                                  "var FIRMWARE = document.getElementById(\"firmware\"); " +
                                  "var UPLOAD_ACTION = document.getElementById(\"upload_action\");" +
                                  " if (FIRMWARE.value == \"\")" +
                                       " alert(\"Please select firmware file\");" +
                                  " else" +
                                  "{" +
                                      "fileSize = 47599364;" +
                                      "" +
                                      "if (fileSize > 0)" +
                                      "{" +
                                           "if (uploading == 0)" +
                                           "{" +
                                                 "document.forms[\"myForm\"].submit();" +
                                                  "setTimeout(\"firmware_update_query_request();\", POLLING_FW_STATUS_SECONDS * 1000);" +
                                                  "document.getElementById('upgradeFrame').style.display = 'none';" +
                                                  "document.getElementById('upgradeModalBox').style.display = 'block';" +
                                                  "UPLOAD_ACTION.value = \"Cancel\";" +
                                                  "uploading = 1;" +
                                          "}" +

                                          "else" +
                                          "{" +
                                                  "uploading = 0;" +
                                                  "UPLOAD_ACTION.value = \"Update\";" +
                                                  "myForm.reset();" +
                                          "}" +
                                       "}" +
                                   "}" +
                               "}";

        headElement.AppendChild(scriptElement);
        webBrowser1.Document.InvokeScript("upgradeRouter_M");
        Console.WriteLine("Injection completed");
    }
MikeRays
  • 23
  • 6