4

I am using Blueimp and server side is Java, Struts2. I couldn't find examples using Java, anyway I managed to use the sample code, but I am getting "Empty file upload result" when I am trying to upload a single file also. The HTML part is the same, I am not pasting here as it may go lengthy.

The jQuery is:

$(document).ready(function () {
    'use strict';

    // Initialize the jQuery File Upload widget:
    $('#fileupload').fileupload();

    // Enable iframe cross-domain access via redirect option:
    $('#fileupload').fileupload(
        'option',
        'redirect',
        window.location.href.replace(
            /\/[^\/]*$/,
            '/cors/result.html?%s'
        )
    );

    if (window.location.hostname === 'blueimp.github.com') {
        // Demo settings:
        $('#fileupload').fileupload('option', {
            url: '//jquery-file-upload.appspot.com/',
            maxFileSize: 5000000,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            process: [
                {
                    action: 'load',
                    fileTypes: /^image\/(gif|jpeg|png)$/,
                    maxFileSize: 20000000 // 20MB
                },
                {
                    action: 'resize',
                    maxWidth: 1440,
                    maxHeight: 900
                },
                {
                    action: 'save'
                }
            ]
        });
        // Upload server status check for browsers with CORS support:
        if ($.support.cors) {
            $.ajax({
                url: '//jquery-file-upload.appspot.com/',
                type: 'HEAD'
            }).fail(function () {
                $('<span class="alert alert-error"/>')
                    .text('Upload server currently unavailable - ' +
                            new Date())
                    .appendTo('#fileupload');
            });
        }
    } else {
        // Load existing files:
        $('#fileupload').each(function () {
            var that = this;
            $.getJSON(this.action, function (result) {
                if (result && result.length) {
                    $(that).fileupload('option', 'done')
                        .call(that, null, {result: result});
                }
            });
        });
    }

});

The action:

@Namespace("/")
@InterceptorRefs({
  @InterceptorRef("fileUpload"),
  @InterceptorRef("basicStack")
})
public class UploadAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{

    HttpServletRequest req;
    HttpServletResponse res;
  //  private File fileUploadPath=new File("c:\\temp\\");
    private List<File> uploads = new ArrayList<File>();
    private List<String> uploadFileNames = new ArrayList<String>();
    private List<String> uploadContentTypes = new ArrayList<String>();

    public List<File> getUploads() {
        return uploads;
    }

    public void setUploads(List<File> uploads) {
        this.uploads = uploads;
    }

    public List<String> getUploadFileNames() {
        return uploadFileNames;
    }

    public void setUploadFileNames(List<String> uploadFileNames) {
        this.uploadFileNames = uploadFileNames;
    }

    public List<String> getUploadContentTypes() {
        return uploadContentTypes;
    }

    public void setUploadContentTypes(List<String> uploadContentTypes) {
        this.uploadContentTypes = uploadContentTypes;
    }
    
    @Action(value="upload", results = { @Result(name="success", type="json")
    })
    public String uploadFiles() throws IOException
    {
        System.out.println("upload1");
        System.out.println("files:");
        for (File u: uploads) {
            System.out.println("*** "+u+"\t"+u.length());
        }
        System.out.println("filenames:");
        for (String n: uploadFileNames) {
            System.out.println("*** "+n);
        }
        System.out.println("content types:");
        for (String c: uploadContentTypes) {
            System.out.println("*** "+c);
        }
        System.out.println("\n\n");
        if (!ServletFileUpload.isMultipartContent(req)) {
            throw new IllegalArgumentException("Request is not multipart, please 'multipart/form-data' enctype for your form.");
        }
        return SUCCESS;
    }
    
    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        this.req=hsr;
    }

    @Override
    public void setServletResponse(HttpServletResponse hsr) {
        this.res=hsr;
    }
    
}

As I said, I have changed the action file, but I still get all empty values for files, and in the Firebug's GET response I see "Request is not multipart, please 'multipart/form-data' enctype for your form".

Roman C
  • 49,761
  • 33
  • 66
  • 176
Aadam
  • 1,521
  • 9
  • 30
  • 60

1 Answers1

5

You may use fileUpload interceptor to parse your "multipart/form-data" requests. It uses the same commons-fileupload implementation wrapped by the MultipartRequestWrapper in prepare operations by the Struts2 dispatcher. More about how to file upload with examples you could find here.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • you mean what i have done above is wrong? That is not going to work with struts2? – Aadam Apr 21 '13 at 04:19
  • k, in firebug i can see this, now how to solve that, in form i have given enctype="multipart/form-data", y its not taking :@ – Aadam Apr 21 '13 at 04:54
  • Yes, you should put ` – Roman C Apr 21 '13 at 08:48
  • i even did that, still same – Aadam Apr 21 '13 at 10:13
  • Consider that the limits you are setting (20MB, etc) are client side, but you need to configure them server side for the FileUpload Interceptor (maxFileSize) and for the whole request in struts.xml (MultiPartMaxSize): http://stackoverflow.com/questions/15957470/struts2-sform-element-trims-the-surl-parameter-in-the-action-attribute/15968166#15968166 – Andrea Ligios Apr 21 '13 at 13:21
  • @user2003821 In the form you need the input element type file with the name `Uploads` the same as setter the value of this element should point to the file stored in the file system before submit. – Roman C Apr 21 '13 at 14:11
  • i am using annotations, dont have struts.xml – Aadam Apr 21 '13 at 16:33
  • you use Struts.xml the same for the shared resources (global mappings, configuration), annotations are only for local resources (Actions) – Andrea Ligios Apr 22 '13 at 10:20