2

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

  • Because you should be using `getPart()` instead on `multipart/form-data` requests. Duplicate of [How to upload files in JSP/Servlet?](http://stackoverflow.com/questions/2422468/how-to-upload-files-in-jsp-servlet/2424824#2424824) – BalusC Feb 07 '12 at 15:48
  • @BalusC thanks for the response. As my question is not duplicate and i am using DWR for file uploading, i edited my question. Sorry i didn't mentioned it earlier. –  Feb 08 '12 at 08:04
  • The way how you parse `multipart/form-data` requests in the servlet is **not** different. – BalusC Feb 08 '12 at 11:16
  • @BalusC Oh Man! i am not having any problem regarding file uploading. File is uploaded successfully. But when i submit form, then the parameter is null. –  Feb 08 '12 at 13:14
  • OK, I give up. Good luck figuring the problem. You might want to re-read the linked dupe more carefully though. – BalusC Feb 08 '12 at 13:17
  • I didn't find where you are setting the value of hidden parameter 'fileName' in your code. – Hardik Mishra Feb 10 '12 at 08:41
  • @HardikMishra Pls don't get confused with that hidden field. It is set on page load, for your convenience i had set a default value. –  Feb 10 '12 at 08:59
  • Ok.. As Per BalusC 's comment. When you use "enctype="multipart/form-data" in you form tag, It will always give NULL when you use request.getParameter("fileName"). – Hardik Mishra Feb 10 '12 at 10:40
  • @HardikMishra You can see that in my main form there is no enctype. –  Feb 10 '12 at 10:49
  • @HardikMishra Here 'fileName' field was a bit confusing so i changed the name to 'UserName' –  Feb 10 '12 at 16:05
  • Since you have already uploaded the image, why don't you simply feed an hidden field and use it on the server side to grab the previously uploaded file? – fcracker79 Jan 15 '15 at 11:07

0 Answers0