views:

1373

answers:

4

Hello,i just started using Uploadify flash plugin instead of standard HTML UI.
And met the next problem:

when I click "Upload Files" link,that progress is shown and "completed" status is appeared, but in reality - it didn't happened anything,Java Servlet isn't called from backend.

There is upload servlet and uploading performed next way earlier:

< form enctype="multipart/form-data" method="post" target="uploadFrame"
action="<%= request.getContextPath() %>/uploadFile?portletId=${portletId}&remoteFolder=${remoteFolder}">...

After providing Uploadify plugin, UI now looks like:

plugin part(configuration):

    <script>
...  
         oScript.text+= "$j('#uploadify').uploadify({";
      oScript.text+= "'uploader' : 'kne-portlets/js/lib/uploadify/scripts/uploadify.swf',";
      oScript.text+= "'script'   : '<%= request.getContextPath() %>/uploadFile?portletId=${portletId}&remoteFolder=<%= decodedString %>',";
      oScript.text+= "'cancelImg': 'kne-portlets/js/lib/uploadify/cancel.png',";
      oScript.text+= "'folder'   : '<%= decodedString %>',";
      oScript.text+= "'queueID'  : 'fileQueue',";
      oScript.text+= "'auto'     : false,";
      oScript.text+= "'multi'    : false,";
      //oScript.text+= "'sizeLimit' : 1000";
      oScript.text+= "});";
      oScript.text+= "});"; 
...   
</script>

'scripts' parameter here points to Java Servlet on backend

<%= decodedString %> is folder path, which value is \\file-srv\demo

part for uploading:

<input type="file" name="uploadify" id="uploadify" />
<a href="javascript:$j('#uploadify').uploadifyUpload();">Upload Files</a>

Where is my fault?

'Script' param in plugin config points to Java Servlet on backend and it's done,but Servlet isn't triggered.

error, when 'script' param isn't correct:http://img190.imageshack.us/i/errormm.png/

Thank you for assistance.

+1  A: 

This can have a lot of possible causes (also see the comments I posted).

  • External JS is not loaded.
  • JS code is syntactically/logically invalid.
  • Request URL is invalid.
  • Servlet is not mapped at all.
  • Servlet is mapped on wrong url-pattern.
  • Servlet failed to start/init.

It's hard to naildown the root cause based on the as far given information.

As you mentioned that you didn't see any request been fired in the "Net" tab of FireBug, I think that the JS code is simply syntactically/logically invalid. Rightclick page and doubleverify generated/printed JS code.

Update: I tried to reproduce your problem.

  1. I downloaded jquery.uploadify-v2.1.0 (MIT), extracted it and put the entire contents in the /WebContent/uploadify folder of my (empty) playground web project in Eclipse.

  2. I created a /WebContent/upload.jsp file as follows:

    <!doctype html>
    <html lang="en">
        <head>
            <title>Uploadify test</title>
            <script src="uploadify/jquery-1.3.2.min.js"></script>
            <script src="uploadify/swfobject.js"></script>
            <script src="uploadify/jquery.uploadify.v2.1.0.min.js"></script>
            <script type="text/javascript">
                $(document).ready(function() {
                    $('#uploadify').uploadify({
                        'uploader': 'uploadify/uploadify.swf',
                        'script': 'uploadServlet',
                        'folder': '/uploads',
                        'cancelImg': 'uploadify/cancel.png'
                    });
                    $('#upload').click(function() {
                        $('#uploadify').uploadifyUpload();
                        return false;
                    });
                });
            </script>
        </head>
        <body>
            <input id="uploadify" type="file">
            <a id="upload" href="#">Upload</a>
        </body>
    </html>
    
  3. I created a com.example.UploadServlet as follows with little help of Apache Commons FileUpload (just placed commons-fileupload-1.2.1.jar and commons-io-1.4.jar in /WEB-INF/lib):

    package com.example;
    
    
    import java.io.IOException;
    import java.util.List;
    
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    
    
    public class UploadServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
        {
            System.out.println("UploadServlet invoked. Here are all uploaded files: ");
            try {
                List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
                for (FileItem item : items) {
                    if (!item.isFormField()) {
                        System.out.println("Name: " + item.getName());
                        System.out.println("Size: " + item.getSize());
                        System.out.println("Type: " + item.getContentType());
                    }
                }
            } catch (Exception e) {
                throw new ServletException(e);
            }
        }
    }
    
  4. I registered com.example.UploadServlet in web.xml as follows:

    <servlet>
        <servlet-name>uploadServlet</servlet-name>
        <servlet-class>com.example.UploadServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>uploadServlet</servlet-name>
        <url-pattern>/uploadServlet</url-pattern>        
    </servlet-mapping>
    
  5. I deployed the project, started the server, went to http://localhost:8080/playground/upload.jsp, selected a random big file from my downloads folder, clicked the Upload link, see the upload percentage counter growing to 100% and I finally see the following in the stdout:

 UploadServlet invoked. Here are all uploaded files: 
 Name: glassfish-v3-windows.exe
 Size: 50402555
 Type: application/octet-stream
 

I'm sorry to say, I can't reproduce your problem. At least, the above information should help you to get started "freshly". Hope it helps.

Update: as per the comments, the filter expects that it is using the same session. Ok, you can do this fairly easy by changing

'<%= request.getContextPath() %>/uploadFile?portletId=${portletId}&remoteFolder=<%= decodedString %>',";

to

'<%= request.getContextPath() %>/uploadFile;jsessionid=${pageContext.session.id}?portletId=${portletId}&remoteFolder=<%= decodedString %>',";
BalusC
Seems,that 'script' param is really taken into consideration,because if it refers to non-existing servlet, that HTTP Error message is appeared on UI.Actually, i couldn't find this message in plugin sources.See,please, screen in bottom of the post.
sergionni
Okay. *How* did you conclude that the servlet isn't triggered? You literally stated like that in your question. Have you for instance debugged the `doPost()` method? Even the poor man's way with a `System.out.println("Hey, uploadservlet doPost() is invoked!");`? Have you for instance read **all** server logs in the `/logs` folder?
BalusC
I just discovered,with help of sniffer,that there is no POST request at all on click for uploading file,i.e. Servlet isn't triggered.Seems, that problem really in Flash config,so in JS as you said.
sergionni
sorry, didn't answer your first question in the comment. I concluded, that Servlet isn't called with help of debug.Earlier, when UI was just form with action=Servlet ,that was ok and Servlet method handleRequest was called OK.method is handleRequset, because Servlet class implements org.springframework.web.HttpRequestHandler;
sergionni
Debug the JS code. More can I really not be of assistance. Use Firebug or Venkman JS Debugger.
BalusC
Oooppph!!! BalusC, it was really hard work. I stuck and asked my TL to help me. Because, i implemented UI part,that i forgot in my debug activity? about filter in another class. And that filter(java method) got null session ID. So this is JsessionID issue,i should share session's value.here is explanation:http://request.getattribute.com/2009/09/14/17/I will be working on it...
sergionni
You're absolutely correct about jsessionid.So the root cause is that Flash doesn't set cookies.And now i got another problem - Uploadify cant' pass more than one param in 'script', but i think that's another question. This issue is resolved.
sergionni
A: 

When I selected 'upload.jsp' file it is just displaying link 'upload' when I press this link nothing happens, my question is that where we are assigning file so that it can be uploaded??

Can you please post revised 'upload.jsp' which accepts file input for upload.

Thanks, Sharath.

sharath
you should have servlet on backend which will handle upload, it's common apache lib
sergionni
A: 

I'm facing the same issue... And it's even more strange, I have it working on my local environment, but when move to a TEST server it isn't working any more.

By not working I mean:

  • I've the indication that file is uploaded
  • My JAVA action is not called (got some print in it)
  • No javascript error

The javascript is loaded correctly (tried some alert), but the upload is not called properly.

Here is the uploadify call:

$(document).ready(function() {
$("#uploadify").uploadify({
    'uploader'       : 'js/uploadify/uploadify.swf',
    'script'         : '/RepairCenter/Dispatcher?REQUEST=IM_UPLOAD_ACTION',
    'cancelImg'      : 'imgs/cancel.png',
    'folder'         : '/upload',
    'queueID'        : 'fileQueue',
    'auto'           : true,
    'multi'          : true,
    'fileExt'        : '*.jpg',
    'fileDesc'       : 'Files (*.jpg)'
});

});

Could this be some kind of path issue, which is the only thing that cna change between the 2 servers? When I call directly "http://myserverurl/scriptparameter the JAVA ACTION is called properly.

Need to understand how to see what call is the SWF really doing..

If you have any suggestions please let me know. Thanks in advance.

Laurent
I found the problem: my test server is a secured env where sessions are protected by an authenication form. The SWF file doesn't share the user session, which then is prompted to login again while trying to reach the 'script' action.
Laurent
A: 

did anyone get multiple file uploads working? I am using 'multi': true ... but the servlet only gets one file passed to it, i can upload 3 files, and only 1 will be saved ... i believe the POST is only submitting 1 file according to Fiddler...

Does anyone have a java servlet Uploadify example working with multiple files?

Here is my Class that just outputs the filename(s) [only system.out.prints 1 file though]:

package com.test.uploadify;

import java.util.List;

import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


public class UploadServlet extends HttpServlet {

  @SuppressWarnings("unchecked")
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {

    System.out.println("UploadServlet invoked. Here are all uploaded files: ");   

    try {

        List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);          

        //request.getParameter('Filedata');

        for (FileItem item : items) {

            if (!item.isFormField()) {
                System.out.println("Name: " + item.getName());
                System.out.println("Size: " + item.getSize());
                System.out.println("Type: " + item.getContentType());

                /*
                File fullFile  = new File(item.getName()); 
                String filename = item.getName();
                File targetdir = new File("C:/");
                File savedFile = new File(targetdir,fullFile.getName());
                item.write(savedFile);
                */

            }
        }
    } catch (Exception e) {
        throw new ServletException(e);
    }
  }
}

and my html file/jquery/Uploadify code:

<!doctype html>
<html lang="en">
    <head>
        <title>Arian - Uploadify Test</title>
        <script src="scripts/jquery/jquery-1.3.2.min.js"></script>
        <script src="scripts/jquery/swfobject.js"></script>
        <script src="scripts/jquery/jquery.uploadify.v2.1.0.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {

                $('#uploadify').uploadify({
                    'uploader': 'scripts/jquery/uploadify.swf',
                    'script': 'uploadServlet',
                    'multi': true,
                    'scriptData': {token: '1234'},
                    'cancelImg': 'scripts/jquery/cancel.png'
                });

                /* 'auto': true, */

                $('#upload').click(function() {
                    $('#uploadify').uploadifyUpload();
                    return false;
                });

            });
        </script>
    </head>
    <body>
        <input id="uploadify" type="file" />
        <a id="upload" href="#">Upload</a>
    </body>
</html>
armyofda12mnkeys
Welcome at Stackoverflow! This look more like a question than an answer. If this is true, then you should use the `Ask Question` button at top to post it :) Else you're just adding noise to other's question and nobody can reasonably answer your question. Feel free to include links in your question which you found in search, but didn't help much.
BalusC