使用uploadify v3.2.1上传文件时无法传递其他值

时间:2013-10-13 09:43:28

标签: java uploadify form-data

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

我总是得到 Here 1 null ,这意味着 null fileName 值。任何人都帮助我,弄清楚,我该怎么做才能正确地获得价值。

1 个答案:

答案 0 :(得分: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
                    }
                });
            });

并在上传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
   }