Search code examples
javascriptfine-uploader

Fine-uploader - add an asyc request to server before uploading each file?


I am using Fine-uploader (traditional version) trying to let users upload files to our own server. The challenge I am having now is that I am supposed to get a token from server first before normal uploading process. The token will be sent along with each uploading request/chunk). This token is unique to each file, so this "request-for-token" step can start only after user has chosen a file.

I was thinking about putting something into "onSubmit" callback. But this "request-for-token" is an asyc call, which means the server's response will not come back right away. I don't know how to do it, or if that is the good way to do this.

Thanks for any help.


Solution

  • The onSubmit callback accepts a "thenable" return value. In other words, you can return a Promise, and Fine Uploader will wait before proceeding to the next step for that file until the returned promise is either resolved or rejected. For example:

    var uploader = new qq.FineUploader({
       callbacks: {
          onSubmit: function(id, name) {
             return new Promise(function(resolve, reject) {
                // Send request to server, resolve() on success
                // ... reject() on failure.
                // Call this.setParams(tokenData, id)
                // ...to send the token w/ the upload request.
             })
          }
       }
    })