2013-10-13 56 views
0

的index.jsp無法通過額外的價值,而使用uploadify V3.2.1

<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(); 
} 
} 

我一直都想與Here 1 null,這意味着null值爲fileName。任何人都可以幫助我,弄清楚,我應該怎麼做才能正確地獲得價值。

回答

0

最後..大量的谷歌搜索後,我找到了解決方案。所以,我共享相同的,以防萬一,如果有人需要同樣的事情在未來

我改變了javascript以下:上傳的servlet的內部

$(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 
        } 
       }); 
      }); 

,並添加以下代碼

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 
    }