4

I would like to set the request endpoint of fine-uploader dynamically. For example:

$('#jquery-wrapped-fine-uploader').fineUploader({
      request: {
        endpoint: submitUrl
      },
      multiple: false,
      button: $("#uploader-button"),
      // etc...
  });

submitUrl must be already set in the page and can't be changed. I would like to do something more dynamic, something like:

 request: {
        endpoint: function() { ($('#submitUrlField').val() }
      }

But the above sends the request to function%20()%20%7B%20$(%27#submitUrlField%27).val()%20}

Grateful for anyone who knows how to do this!

bz3x
  • 171
  • 4
  • 15

4 Answers4

2

You can use multi enpoints. For example use several endpoint for odd/even uploads:

        callbacks: {
            onUpload:  function(id, name) {
                var endpoint = endpoints[id % endpoints.length];
                this.setEndpoint(endpoint.url, id);
                this.setParams({
                    signature: endpoint.signature,
                    params: endpoint.params,
                    ajax: true
                }, id);
            }
        }
Hett
  • 3,484
  • 2
  • 34
  • 51
  • 1
    You can replace `uploader` with `this` if you'd like as well. – Ray Nicholus Mar 31 '16 at 14:09
  • I'm not sure if my eyes were deceiving my when I first read your answer, but there are a number of issues with it that I see now. Why did you add a global variable (bad)? Why is your endpoint url not a url? Why are you logging to the console when a comment is more appropriate in this context? – Ray Nicholus Apr 01 '16 at 17:06
  • 1
    @RayNicholus it just a example ;) – Hett Apr 01 '16 at 17:22
  • ...but why would you ever want to set the endpoint to "odd" or "even", or perform a modulus on the id? It seems like noise to me. Reducing this to the single call to setEndpoint seems more appropriate. – Ray Nicholus Apr 01 '16 at 17:24
0

If the file is big and you have enabled the chunking, you can't change the endpoint url dinamically. My solution is use the event

callbacks:{
  onComplete:function(id,name,responseJSON,xhr) {
    console.log('> onComplete. id=' + id);
    console.log('> onComplete. name=' + name);
    console.log('> onComplete. responseJSON=' + JSON.stringify(responseJSON));
    console.log('> onComplete. uuid=' + responseJSON.uuid);

    if (responseJSON.success)
    {
      // call a httpRequest with the url you want.
    }
  }
}
karrtojal
  • 796
  • 7
  • 16
0

One useful example for the setEndpoint method. This code will set endpoint based on uploading file type:

    callbacks: {
        onUpload:  function(id, name) {
            var file = this.getFile(id);
            var fileType = file.type;
            this.setEndpoint(endpointList[fileType], id);
        }
    }

Your endpointList:

var endpointList = {
    'image/png': 'url/uploadimage',
    'video/mp4': 'url/uploadvideo',
    'text/plain': 'url/uploadtext'
}
Harry
  • 678
  • 10
  • 26
-2

your old code:

 request: {
        endpoint: function() { ($('#submitUrlField').val() }
      }
                               |_ why need ( becasue it has not close ) match

try one of these

request: {
   endpoint: function() { return $('#submitUrlField').val() }  // here you had `(`
}

or

request: {
   endpoint: $('#submitUrlField').val() 
}
Snake Eyes
  • 16,287
  • 34
  • 113
  • 221
  • Thanks! I also had to remember not to define the jquery-wrapped-fine-uploader directly in `$(document).ready(function () {` but in a function that is called after I change the value of submitUrlField – bz3x Oct 12 '13 at 13:53
  • 1
    The endpoint option does not accept a function as a value, so only the last example will work. – Ray Nicholus Oct 12 '13 at 14:11
  • 1
    Also note that Fine Uploader provides a setEndpoint API method that allows you to set the endpoint for all files or a specific file at any time after the Uploader instance has been created. See the [methods documentation page](http://docs.fineuploader.com/api/methods.html) for details. – Ray Nicholus Oct 12 '13 at 14:15
  • @Ray Nicholus. Your comment about the setEndpoint API method should be an answer! Thanks. – gordon613 Apr 26 '18 at 15:47