PDA

View Full Version : standard form submit with jsonData for file download



pennstatephil
13 Mar 2015, 1:22 PM
I'm trying to download a file from WebApi using ExtJs 4.2.3. After reading Extjs 4 downloading a file through ajax call (http://stackoverflow.com/questions/20499959/extjs-4-downloading-a-file-through-ajax-call), it looks like my best bet is to use the standard form.submit, however, my data is not passing as expected to the WebApi controller - using the below code, items comes through null.

Controller

public HttpResponseMessage Post(string exportType, List<PcAvailableComponent> items){
var dataToExport = _service.ExportItems(exportType, items);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new MemoryStream(dataToExport);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
return result;
}

Standard Submit


expForm.submit({ url: AppRootUrl + 'api/AdminDataExport?exportType=' + exportType,
//this doesn't work...
jsonData: items,
waitMsg: 'Generating export...',
success: function (form, action) {
if (typeof expWindow !== "undefined") {expWindow.close();}
},
failure: function(form, action) {
Ext.Msg.alert('Failed', 'An error has occurred while generating the export: ' + JSON.parse(action.response.responseText)['ExceptionMessage']);
}
});

Ajax submit (works but can't get file back)

Ext.Ajax.request({ url: AppRootUrl + 'api/AdminDataExport',
params: {
exportType: 'PricingAndCosting'
},
jsonData: items,
method: 'POST',
success: function (response) {
expWindow.close();
console.log("success!");
},
callback: function() {
self.application.fireEvent('setLoading', false);
}
});
Note, this is a clone of http://stackoverflow.com/questions/29041996/extjs-standard-form-submit-with-jsondata-for-file-download but I figured I'd try here, also.

Gary Schlosberg
13 Apr 2015, 12:48 PM
Were you able to get one of those solutions to work for you? Creating an invisible form and submitting it seems like a good approach, especially coming from Alex.

pennstatephil
15 Apr 2015, 8:43 AM
The best solution I found was to change the parameter in the controller to a string rather than a List, and then use JsonConvert.DeserializeObject. See http://stackoverflow.com/a/29081807/347455

Gary Schlosberg
27 Apr 2015, 2:59 PM
Interesting. Thanks for sharing the solution with the community!