Question

index.jsp

<h1>Uploadify Demo</h1>
        <form id="uploadForm">
            <div id="queue"></div>
            <input id="file_upload" name="file_upload" type="file" multiple="true">
            <input type="text" id="fileName" name="fileName"/>
        </form>

        <script type="text/javascript">

            $(function() {
                $('#file_upload').uploadify({                    
                    'swf'      : 'uploadify.swf',
                    'uploader' : '../upload',                   
                    'formData': { 'fileName': $('input:text[name:fileName]').val() }
                });
            });
        </script>

upload.java

  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {

    final String UPLOAD_DIRECTORY = request.getSession().getServletContext().getRealPath("/upload");
    //process only if its multipart content
    if (ServletFileUpload.isMultipartContent(request)) {
        try {
            List<FileItem> multiparts = new ServletFileUpload(
                    new DiskFileItemFactory()).parseRequest(request);

            for (FileItem item : multiparts) {
                if (!item.isFormField()) {
                    String name = new File(item.getName()).getName();
                    item.write(new File(UPLOAD_DIRECTORY + File.separator + name));
                }else{
                System.out.print("Here 1 "+request.getParameter("fileName"));
                }

            }

            //File uploaded successfully
            request.setAttribute("message", "File Uploaded Successfully");
        } catch (Exception ex) {
            request.setAttribute("message", "File Upload Failed due to " + ex);
        }

    } else {
        request.setAttribute("message",
                "Sorry this Servlet only handles file upload request");

    }


   // request.getRequestDispatcher("/result.jsp").forward(request, response);


} catch (Exception e) {
    e.printStackTrace();
} finally {
    out.close();
}
 }

I'm always getting Here 1 null, which means null value for fileName. Anyone help me, to figure out, what should I do to get the value correctly.

Was it helpful?

Solution

Finally.. after lot of googling I found the solution. So, I'm sharing the same, just in case if anyone needs same thing in future

I changed the javascript to below :

$(function() {
                $("#file_upload").uploadify({
                    'formData'      : {'fileName' : 'hello', 'fileNum' : 1},
                    'swf'           : 'uploadify.swf',
                    'uploader'      : '../upload',
                    'onUploadStart' : function(){
                        $("#file_upload").uploadify('settings','formData' ,{'fileNum': $('#fileNum').val()}); // to change value dynamically
                    }
                });
            });

and added following code inside upload servlet

if (item.isFormField()) {
    System.out.println(item.getFieldName()); // field name
   System.out.println(item.getString());  // and field value
   // Here we can retreive all the Form Data
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top