【问题标题】:Read file path from javascript variable and send it to form action?从 javascript 变量中读取文件路径并将其发送到表单操作?
【发布时间】:2014-06-12 13:52:29
【问题描述】:

代码:

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

<script type="text/javascript">

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

</script>

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>

我尝试了 3 种不同的方法来通过 +filename+ 传递它

请指教

【问题讨论】:

    标签: java html jsp servlets web-applications


    【解决方案1】:

    尝试类似:

    function showFileName() { 
        document.forms[0].action ="<%=Constants.WEB_APP_NAME%>... //Here goes the expression you want to set the action to
    }
    

    【讨论】:

    • 我正在尝试这样,但是当我点击添加附件时,操作没有发生
    • 不要使用 type=submit 的输入,使用按钮,并通过 javaScript 代码进行提交。
    • 我再次使用 uploadFilefunction showFileName() { var filename = document.getElementById("uploadFile"); 获得空值document.forms[2].action ="?para=ajaxRefTabUpload&action=add&uploadfilename="+filename+""; document.AttachmentsForm.submit(); }
    【解决方案2】:

    强烈建议不要在 JSP 中使用 scriptlet(&lt;% %&gt;),请使用 JSTL(&lt;c:tags) 或表达式语言(${})

    在您的 jsp 中使用 JSTL 下载 jstl.x.x.jar 文件并将其添加到您的构建路径中。

    然后,添加这个

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

    在 jsp 的顶部。


    解决您拥有的表单操作网址:

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

    不需要任何常量,而是使用&lt;c:url标签来解析url,如:

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

    我在 action 参数中使用 uploadfilename 得到 null

    因为,您在 html(&amp;uploadfilename="+filename+") 中访问 javascript 变量,而不是喜欢:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-04
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      相关资源
      最近更新 更多