Uploaded image for project: 'Nuxeo Elements'
  1. Nuxeo Elements
  2. ELEMENTS-370

Add blob utils to help handle blob download through ajax

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: QualifiedToSchedule
    • Component/s: Core

      Description

      Currently, executing an operation that returns a blob is troublesome, because the request is made via ajax an there is no easy way to trigger the download on the browser. Currently, we are always replicating the same response handling logic to trigger the download, which should be made available to prevent code duplication:

      // execute operation
      _executeOperation: function() {
        return this.$.operation.execute().then(function(response) {
          return this._download(response).then(function() {
            // success
            this.fire('document-updated');
          }.bind(this));
        }.bind(this)).catch(function(response) {
          // something went wrong
        }.bind(this));
      },
      
      // trigger download
      _download: function(response) {
        var contentDisposition = response.headers.get('Content-Disposition');
        if (contentDisposition) {
          var filenameMatches = contentDisposition
              .match(/filename[^;=\n]*=([^;\n]*''([^;\n]*)|[^;\n]*)/).filter(function(match) { return !!match; });
          var filename = decodeURI(filenameMatches[filenameMatches.length - 1]);
          return response.blob().then(function(blob) {
            if (navigator.msSaveBlob) {
              // handle IE11 and Edge
              navigator.msSaveBlob(blob, filename);
            } else {
              var a = document.createElement('a');
              a.style = 'display: none';
              a.download = filename;
              a.href = URL.createObjectURL(blob);
              document.body.appendChild(a);
              a.click();
              document.body.removeChild(a);
              URL.revokeObjectURL(a.href);
            }
          }.bind(this));
        } else {
          return Promise.reject(new Error('missing Content-Disposition header'));
        }
      }
      

      We're currently using this on nuxeo-template-rendering, but this code should be refactored.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                gbarata Gabriel Barata
                Participants:
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated: