0

How to submit the form on click of button to call function inside javascript call with all information of form given below. I am using struts core tag library and add button tag. Below is the code

<body>
    <html:form action="createtableAction.do?param=uploadExcelFile1" method="post" enctype="multipart/form-data">
        <table>
            <tr>
                <td align="left" colspan="1" class="migrate">
                    <html:file property="filename" styleId="filename" style="background-color:lightgoldenrodyellow; color:black;" />
                </td>
            </tr>
            <tr style="width:100%">
                <td align="center" colspan="2" class="migrate">
                    <html:submit onclick="return Checkfiles()" style="background-color:silver; color:darkblue;">Upload File</html:submit>
                </td>
            </tr>
        </table>
    </html:form>
</body>

in function call action and submit

function Checkfiles() {
    if (fileList[count] === tempFileName) {
        //want action and form submit code ...
        //document.forms[0].action='/createtableAction.do?param=uploadExcelFile1';
        //document.forms[0].submit();
        return true;
    } else {
        return false;
    }    
}
Amit.rk3
  • 2,417
  • 2
  • 10
  • 16

1 Answers1

0

You can send the file using ajax request. There is FormData()available in javascript. You can append your files to the FormData object and send it to servlet.

function checkFiles(){
     var formData=new FormData();
     var uploadUrl = 'createtableAction.do?param=uploadExcelFile1';
     // Uncomment below this if you wish to send any other fields with file
     // formData.append('id', 1);
     console.log('loading file');
     var file= document.getElementById("filename"); 
     formData.append('filename',file);

     //create the ajax request (traditional way)
     var request = new XMLHttpRequest();
     request.open('POST', uploadUrl);
     request.send(formData);
     // if you want to reload uncomment the line below
     // window.location.reload();
}

Hope this helps you.

Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34