0

I have an extjs ajax function that send a form to the server which returns a file to download with the correct Content-Disposition ="attachment; filename='test.zip'" header. The file can be downloaded with the normal file download window of the browser. However, the success callback function of the ajax request is not triggered and the ajax is pending indefinately waiting for a response.

Is there a way to tell the ajax function that the file was correctly sent from the server or not?

  exportLayer = function(node_id){
    Ext.Ajax.request({
      method: "GET",
      form: "exportForm",
      url: "/basqui/layer/shapefile/export/" + node_id + "/",
      success: function(r){
                  html = Ext.decode(r.responseText).html
                  Ext.get('pageContent').update(html);
                  },
    });
  }
Below the Radar
  • 7,321
  • 11
  • 63
  • 142

1 Answers1

1

Set binary configuration property of Ext.Ajax.request and in the response you would get the binary data inside responseBytes property.

For example loading jpeg image:

Ext.Ajax.request({
      url: "/path/to/image/test.jpg",
      success: function(xhr) {
          var b64encoded = btoa(String.fromCharCode.apply(null, xhr.responseBytes));

          var img = Ext.create('Ext.Img', {
              src: 'data:application/jpeg;base64,' + b64encoded
          });
          // add image somewhere
          ...                
      }
 });
babinik
  • 646
  • 3
  • 10
  • not sure it would be efficient with large file, and how can I send it to the download application of the browser? – Below the Radar Jul 04 '14 at 18:16
  • 1
    returning json with validation results and file data base64 encoded would be inefficient for big files, but for small is works. How to download the binary data received via [xhr](http://stackoverflow.com/questions/13752984/html5-file-api-downloading-file-from-server-and-saving-it-in-sandbox), FileAPI and XHR2. But if you want to validate form and then download a file without FileAPI&XHR2 make it in 2 steps: 1. xhr validating the form and 2. receiving the validation results and and being sure all is correct inside the xhr response run the `window.location` dedicated to file download. – babinik Jul 04 '14 at 22:13
  • 1
    my answer mostly was about why your `Ext.Ajax.request` _is not triggered and the ajax is pending indefinately waiting for a response._ Cause request was made to receive the binary data but `binary` configuration option was missed. – babinik Jul 04 '14 at 22:18