Question

code:

<script type="text/javascript">
     function showFileName() {
              var filename = document.getElementById("uploadFile");
      }
</script>

OR

<script type="text/javascript">

      var filename = document.getElementById("uploadFile");

</script>

OR

var filename="uploadedfilename";

<form name="AttachmentsForm" method="post"  action="<%=Constants.WEB_APP_NAME%><%=Constants.SERVLET_NAME%>?para=ajaxRefTabUpload&action=add&uploadfilename="+filename+"" ENCTYPE="multipart/form-data">
    <table class="innerBorderTable" width="100%">
       <tr>                                
    <td>Attach New File:</td>
    <td>
      <INPUT TYPE="FILE" NAME="uploadFile" width="120"> 
      <input type="submit" class="button" value="Add Attachment">
    </td>      
       </tr>
    </table>
</form>

I tried 3 different approaches to pass it with +filename+ i am getting null with uploadfilename in action parameter

Please advise

Was it helpful?

Solution

use of scriptlets(<% %>) in JSP highly discouraged, use JSTL(<c:tags) or Expression Language(${})

to use JSTL in your jsp download jstl.x.x.jar file and add it to your buildpath.

then, add this

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

in top of jsp.


to resolve form action url you have:

<%=Constants.WEB_APP_NAME%><%=Constants.SERVLET_NAME%>

no need of any constants for that, instead use <c:url tag to resolve url like:

<c:url value="/yourServletUrl" var="postUrl"/>
<form action="${postUrl}"  method="post" ...

i am getting null with uploadfilename in action parameter

because, you accessing javascript variable in html(&uploadfilename="+filename+"), instead do like:

jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
  <title>your title</title>
</head>
<body>

<c:url value="/yourServletUrl" var="postUrl"/>
<form id="AttachmentsForm"
      method="post"
      onSubmit="showFileName()"
      action="${postUrl}para=ajaxRefTabUpload&action=add"
      enctype="multipart/form-data">

    <table class="innerBorderTable" width="100%">
       <tr>                                
         <td>Attach New File:</td>
         <td>
           <input type="file" name="uploadFile" width="120"> 
           <input type="submit" class="button" value="Add Attachment">
         </td>      
       </tr>
    </table>
</form>
<script type="text/javascript">
     function showFileName(e) {

         if (e.preventDefault)
             e.preventDefault();

         var filename = document.getElementById("uploadFile");
         alert('fileName: '+filename);

         //here, attach filename to from action and continue your form submition by AJAX

          // You must return false to prevent the default form behavior
          return false;
        }
      }

</script>
</body>
</html>

OTHER TIPS

Try something like:

function showFileName() { 
    document.forms[0].action ="<%=Constants.WEB_APP_NAME%>... //Here goes the expression you want to set the action to
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top