I am trying to use httpClient
to post form data and then get back an excel file.
I have verified via http that the file is a valid excel file, but when I try to read the file, I only get the printed headers from the response.
javascript code:
exportExcel()
{
const self = this;
var url = 'http://[ipaddress]/search.pl';
var formData = new FormData(document.forms['searchform']);
formData.append('REMOTE_USER', self.user);
formData.append('excel', 'open');
formData.append('wltype', this.state.searchObj.wlTypeRadio);
formData.append('agingStyle', this.state.searchObj.agingStyleRadio);
formData.append('custBal', this.state.searchObj.custBalRadio);
formData.append('MODE', this.state.searchObj.mode);
if (typeof(this.state.searchObj.potentialBadDebtRadio) != undefined &&
this.state.searchObj.potentialBadDebtRadio != null)
{
formData.append('badDebt',
this.state.searchObj.potentialBadDebtRadio.value);
}
var filename = this.user + '-';
var dformat;
var d = new Date();
var dformat = [d.getFullYear(), (d.getMonth()+1).toString().padStart(2,'0'),
d.getDate().toString().padStart(2,'0')].join('') +'-' +
[d.getHours().toString().padStart(2,'0'),
d.getMinutes().toString().padStart(2,'0'),
d.getSeconds().toString().padStart(2,'0')].join('');
filename += dformat + '.xlsx';
console.log('filename', filename);
return self.httpClient
.fetch(url, {
body: formData,
method: 'POST'
})
.then(async (response) => response.blob())
.then((blob) =>
download(blob, filename, mime.getType(filename)));
}