How to bind to the uploader to pass custom parameters to FormData

Support General How to bind to the uploader to pass custom parameters to FormData

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #47957
    pluginovenpluginoven
    Participant

    We are using a file_advanced fields and want to pass a custom parameter to FormData so that we can pass this flag to the $_REQUEST and then use the upload_dir filter to better manage where the files are saved.

    While there is a solution of passing a custom parameter when creating an instance of the Ajax Media Uploader, the way media.js it is not clear how to do this the way media.js is creating the media uploader.

    We are able to pass our param to the media uploader, and can even trigger it from the MediaButton click event:

    if (this._frame.uploader) {
        console.log('uploader instance found:', this.controller.get('our_custom_param'));
    } 

    But after quite some digging and trial and error, we can not seem to append the FormData.
    Any tips or recommendations on how this can be done is greatly appreciated. Thanks in advance!

    #47958
    pluginovenpluginoven
    Participant

    After a bit more trial and console.log fun, we have found a solution that works, but it's not pretty.

    In the media.js file under MediaButton > events

    // Refresh content when frame opens
    this._frame.on( 'open', function() {
       // adding the custom parameter to the uploader
       var some_custom_param = this.controller.get('some_custom_param');
       if(some_custom_param){
          this._frame.uploader.uploader.uploader.settings.multipart_params.some_custom_param = some_custom_param;
       }
       var frameContent = this._frame.content.get();
       if ( frameContent && frameContent.collection ) {
          frameContent.collection.mirroring._hasMore = true;
          frameContent.collection.more();
       }
    }, this );

    this is just a test. next steps would be:
    - get feedback on if there is a more elegant way to add the multipart_params
    - create our own field type 'file_more_advanced' so we don't modify metabox's core files
    - OR collaborate on extending the official file_advance field logic to support appending the multipart_params

    Thoughts?

    #47966
    PeterPeter
    Moderator

    Hello,

    Can you please let me know the purpose or a real case to pass the custom param to the form data, specifically the file media.js?

    If you want to upload the file to a custom folder, you can use the field type file. Please follow the documentation https://docs.metabox.io/fields/file/

    #47970
    pluginovenpluginoven
    Participant

    Hello Peter.
    the real case is an advanced plugin that handles handles the uploading, storing and managing access for sensitive data. If you share your github handle, I would be happy to give you access to the repo that explains everything in detail, including a demo-plugin we created as a proof of concept.

    We ended up creating a custom field type called secure_file_advanced that allows the passing of custom parameters to the $_REQUEST before the file is uploaded. To do this we needed to get very intimate with how metabox.io has implemented the WordPress AJAX Media Uploader on the back and frontend (three uploaders deep). Our case may be an edge case, but it dose extend the flexibility of existing file_advanced field.

    Regarding field type file: no for all the reasons that we went with file_advanced in the first place.
    Also, adding the upload_dir attribute to the field meta, visible on the frontend, is not desired.
    Also, with thousands of members we needed more than just a single upload-directory.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.