Menu

Custom post params for ajax upload

iza
2012-12-11
2012-12-12
  • iza

    iza - 2012-12-11

    Hi, I am finding the GWT Uploader library extremely helpful for my GWT project.
    For my application, we require some additional post parameters to be passed along with the file upload (for authentication). I noticed the params set via the SetPostParams method are only used for SWF uploads and not ajax.

    I took the liberty of patching in my desired functionality (below). I also fixed a bug in the exceedsFileSizeLimit check. Previously a limit of 0 was not allowing any files through, when in the documentation it says 0 should be unlimited.

    diff --git a/src/main/java/org/moxieapps/gwt/uploader/client/Uploader.java b/src/main/java/org/moxieapps/gwt/uploader/client/Uploader.java
    index 5efc22c..c331bad 100644
    --- a/src/main/java/org/moxieapps/gwt/uploader/client/Uploader.java
    +++ b/src/main/java/org/moxieapps/gwt/uploader/client/Uploader.java
    @@ -293,6 +293,8 @@ public class Uploader extends AbsolutePanel {
             swfUpload.setFilePostName(filePostName);
         }-*/;
    
    +    private JSONObject postParams;
    +
         /**
          * Convenience method for setting the 'post_params' option of the component, either before or
          * after the widget has been added to the DOM.  Equivalent to:
    @@ -317,6 +319,7 @@ public class Uploader extends AbsolutePanel {
          * @return A reference to this {@link Uploader} instance for convenient method chaining.
          */
         public Uploader setPostParams(JSONObject postParams) {
    +       this.postParams = postParams;
             if (swfUpload != null) {
                 nativeSetPostParams(swfUpload, postParams != null ? postParams.getJavaScriptObject() : null);
             }
    @@ -1259,7 +1262,7 @@ public class Uploader extends AbsolutePanel {
                 // Let any registered progress handlers know that we're starting at the beginning
                 uploadProgressEventCallback(nativeFile.<File>cast(), 0.0, (double) nativeFile.<File>cast().getSize());
    
    -            nativeStartAjaxUpload(nativeFile, ajaxUploadURL != null ? ajaxUploadURL : uploadURL);
    +            nativeStartAjaxUpload(nativeFile, ajaxUploadURL != null ? ajaxUploadURL : uploadURL, postParams != null ? postParams.getJavaScriptObject() : null);
             }
         }
    
    @@ -1268,7 +1271,7 @@ public class Uploader extends AbsolutePanel {
         }-*/;
    
         // See: https://developer.mozilla.org/en/Using_files_from_web_applications
    -    private native void nativeStartAjaxUpload(JavaScriptObject file, String url) /*-{
    +    private native void nativeStartAjaxUpload(JavaScriptObject file, String url, JavaScriptObject params) /*-{
    
             var self = this;
             var xhr = new XMLHttpRequest();
    @@ -1311,6 +1314,9 @@ public class Uploader extends AbsolutePanel {
             xhr.open('POST', url, true);
             var formData = new FormData();
             formData.append('file', file);
    +        
    +        for (var index in params)
    +           formData.append(index, params[index]);
    
             // Kick off the multipart/form-data upload
             xhr.send(formData);
    @@ -1784,7 +1790,7 @@ public class Uploader extends AbsolutePanel {
    
         private boolean exceedsFileSizeLimit(JavaScriptObject nativeFile) {
             long fileSize = nativeFile.<File>cast().getSize();
    -        return this.fileSizeLimit == 0 || fileSize > this.fileSizeLimit;
    +        return this.fileSizeLimit != 0 && fileSize > this.fileSizeLimit;
         }
    
         private void openFileDialog(FileUpload fileUpload, boolean multipleFiles) {
    
     
  • Shawn Quinn

    Shawn Quinn - 2012-12-12

    Glad to hear that GWT Uploader has been working well for you, and thanks for the patch! As irony would have it, we had actually just added support for the "setPostParams" method in Ajax/DHTML mode as well in a test environment here, and ended up taking an approach almost identical to what you show there. We hadn't run across the file size limit issue, so we'll get your fix merged in for that as well.

    So, these changes (plus a couple of other things, including the ability to remove files from the queue and cancel active uploads) will be included in the upcoming release, likely version 1.1.0.

    Thanks again.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.