iframe提交使父表单的请求参数为空

发布于 2025-01-03 15:11:28 字数 3203 浏览 3 评论 0 原文

我在 jsp 中使用隐藏的 iframe 来上传图像文件。代码如下:

  <form name="frm" method="POST" action="./servlet/newSubmit1">
      <div>
           Image:
          <input type="file" id="uploadImage" name="uploadImage" size="30" onchange="uploadFiles(); fileUpload(this.form,'./servlet/TempImageUploader','upload123')"/>
          <input type="hidden" id="UserName" value="TestValue"/>
      </div>
      <div id="upload123"></div>
      <ul>
          <li>  
               <input type="button" value="Upload" tabindex="6" onclick="uploadAttachedPhotos()"/>
          </li>
      </ul>
  </form>

正如您在代码中看到的,调用了浏览按钮“uploadFiles()”和“fileUpload”函数的onchange事件。 uploadFiles函数使用DWR将文件上传到服务器上的临时目录并在jsp页面上预览,fileUplaod函数创建一个隐藏的iframe来调用维护地图中所有上传图像的 servlet。页面上还有一些其他数据可以由用户更新。例如,这里我有“用户名”字段,其中可以包含文件名。

创建隐藏 iframe 的 javascript 函数是:

   function fileUpload(form, action_url, div_id) {
       // Create the iframe...
       var iframe = document.createElement("iframe");
       iframe.setAttribute("id", "upload_iframe");
       iframe.setAttribute("name", "upload_iframe");
       iframe.setAttribute("width", "0");
       iframe.setAttribute("height", "0");
       iframe.setAttribute("border", "0");
       iframe.setAttribute("style", "width: 0; height: 0; border: none;");

       // Add to document...
       form.parentNode.appendChild(iframe);
       window.frames['upload_iframe'].name = "upload_iframe";

       iframeId = document.getElementById("upload_iframe");

       // Add event...
       var eventHandler = function () {

            if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
            else iframeId.removeEventListener("load", eventHandler, false);

           // Message from server...
           if (iframeId.contentDocument) {
               content = iframeId.contentDocument.body.innerHTML;
           } else if (iframeId.contentWindow) {
               content = iframeId.contentWindow.document.body.innerHTML;
           } else if (iframeId.document) {
               content = iframeId.document.body.innerHTML;
           }

           document.getElementById(div_id).innerHTML = content;

           // Del the iframe...
           setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
       }

       if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
       if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);

       // Set properties of form...
       form.setAttribute("target", "upload_iframe");
       form.setAttribute("action", action_url);
       form.setAttribute("method", "post");
       form.setAttribute("enctype", "multipart/form-data");
       form.setAttribute("encoding", "multipart/form-data");

       // Submit the form...
       document.forms[0].action = action_url;
       document.forms[0].submit();

       document.getElementById(div_id).innerHTML = "Uploading...";
 }

现在的问题是,当我按下上传按钮时,为什么我在父表单的操作 servlet 上得到 request.getParameter("UserName") NULL ?

谢谢

I am using hidden iframe in my jsp to upload image files. The code is as following :

  <form name="frm" method="POST" action="./servlet/newSubmit1">
      <div>
           Image:
          <input type="file" id="uploadImage" name="uploadImage" size="30" onchange="uploadFiles(); fileUpload(this.form,'./servlet/TempImageUploader','upload123')"/>
          <input type="hidden" id="UserName" value="TestValue"/>
      </div>
      <div id="upload123"></div>
      <ul>
          <li>  
               <input type="button" value="Upload" tabindex="6" onclick="uploadAttachedPhotos()"/>
          </li>
      </ul>
  </form>

As you can see in the code that onchange event of browse button "uploadFiles()" and "fileUpload" function is called. The uploadFiles function uses DWR to upload file to temp directory on server and preview it on jsp page, and the fileUplaod function creates a hidden iframe to call the servlet which maintains all the uploaded images in a map. I have some other data too on the page which can be updated by the user. For example here i have "UserName" field which can have name of the file.

The javascript function which creates hidden iframe is :

   function fileUpload(form, action_url, div_id) {
       // Create the iframe...
       var iframe = document.createElement("iframe");
       iframe.setAttribute("id", "upload_iframe");
       iframe.setAttribute("name", "upload_iframe");
       iframe.setAttribute("width", "0");
       iframe.setAttribute("height", "0");
       iframe.setAttribute("border", "0");
       iframe.setAttribute("style", "width: 0; height: 0; border: none;");

       // Add to document...
       form.parentNode.appendChild(iframe);
       window.frames['upload_iframe'].name = "upload_iframe";

       iframeId = document.getElementById("upload_iframe");

       // Add event...
       var eventHandler = function () {

            if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
            else iframeId.removeEventListener("load", eventHandler, false);

           // Message from server...
           if (iframeId.contentDocument) {
               content = iframeId.contentDocument.body.innerHTML;
           } else if (iframeId.contentWindow) {
               content = iframeId.contentWindow.document.body.innerHTML;
           } else if (iframeId.document) {
               content = iframeId.document.body.innerHTML;
           }

           document.getElementById(div_id).innerHTML = content;

           // Del the iframe...
           setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
       }

       if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
       if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);

       // Set properties of form...
       form.setAttribute("target", "upload_iframe");
       form.setAttribute("action", action_url);
       form.setAttribute("method", "post");
       form.setAttribute("enctype", "multipart/form-data");
       form.setAttribute("encoding", "multipart/form-data");

       // Submit the form...
       document.forms[0].action = action_url;
       document.forms[0].submit();

       document.getElementById(div_id).innerHTML = "Uploading...";
 }

Now the question is why i am getting request.getParameter("UserName") NULL on the parent form's action servlet, when i press the upload button ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文