2013-10-13 1 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 ниже:

$(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 
    } 
Смежные вопросы