Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had previously faced the same issue. I solved the issue by opening a pop-up when user click on browse button and this new popup was directly linked to a servlet which supports file uploading and downloading. Once a file was uploaded the pop-up was closed directly by the servlet calling window.close() on load of the body of servlet. below are the changes done:</p> <p>In addItem.jsp which prompts user for a file : </p> <pre><code>&lt;script type="text/javascript"&gt; ... function newWindow() { popupWindow = window.open('addItemImage.jsp','name','width=200,height=200'); } ... &lt;/script&gt; &lt;f:view&gt; &lt;h:form id="search" onsubmit="return validateDetails()"&gt; ... &lt;h:panelGroup&gt; &lt;font color="black"&gt;Item Image :&lt;/font&gt; &lt;/h:panelGroup&gt; &lt;h:panelGroup&gt; &lt;input type="button" value="Upload Image" onClick="newWindow()"/&gt; &lt;/h:panelGroup&gt; ... &lt;/h:form&gt; &lt;/f:view&gt; </code></pre> <p>In the new pop-up window that was opened : addItemImage.jsp</p> <pre><code>&lt;script type="text/javascript"&gt; function validate() { var file = document.getElementById("file").value; if(file == null || file == "") return true; if(file.indexOf(".jpg") == -1 &amp;&amp; file.indexOf(".gif") &amp;&amp; file.indexOf(".bmp") == -1 &amp;&amp; file.indexOf(".jpeg") == -1) { alert("Invalid File Format!! Please upload jpg/bmp/gif"); return false; } return true; } &lt;/script&gt; &lt;body&gt; &lt;form enctype="multipart/form-data" method="post" action="FileUploaderServler.view" onsubmit="return validate()"&gt; &lt;input type="file" name="file" id="file" /&gt; &lt;input type="submit" value="Upload Image"&gt; &lt;/form&gt; &lt;/body&gt; </code></pre> <p>This would post to a servlet which would then render the image and save it at a place. FileUploaderServlet.java</p> <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List&lt;FileItem&gt; items = null; try { MultipartHTTPServletRequest multipartHTTPServletRequest = new MultipartHTTPServletRequest(Connection.getRequest()); items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(multipartHTTPServletRequest); } catch (FileUploadException e) { e.printStackTrace(); } if(!Util.isNullList(items)) { for (FileItem item : items) { if (!item.isFormField()) { String itemName = item.getFieldName(); InputStream filecontent = null; try { filecontent = item.getInputStream(); } catch (IOException e) { e.printStackTrace(); } /* String currDirPath = System.getProperty("user.dir"); // C:\Users\KISHORE\Desktop\eclipse File file = new File(currDirPath+"\\itemImages");*/ new File(Constants.ABS_FILE_PATH_TO_IMAGES).mkdir(); File fw = new File(Constants.ABS_FILE_PATH_TO_IMAGES+"\\"+Connection.getRequest().getSession().getId()+".jpg"); // E:\Kishore Shopping Cart\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ShoppingCart\WEB-INF\itemImages\EC34EEE58065AD674192D3D57124F07E.jpg fw.delete(); fw.createNewFile(); try { item.write(fw); } catch (Exception e) { e.printStackTrace(); } } } } PrintWriter out = response.getWriter(); out.print("&lt;html&gt;&lt;title&gt;Add Image to Item&lt;/title&gt;&lt;body onload='window.close()'&gt;Uploaded Successfully!!&lt;/body&gt;"); System.out.print("&lt;/html&gt;"); } </code></pre> <p>This servlet on completion of saving file at a place configured, would directly close the pop-up that was previously opened.</p> <p>So this way I was able to use the managed bean, jsf 1.2 and achieve fiel uploading. I found other ways on stackoverflow but felt it was a bit complex to implement.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload