Answer the question
In order to leave comments, you need to log in
How to download xls file via jquery in correct encoding?
Good afternoon.
On the server, in the Page_Load event, a file is generated:
HttpResponse response = HttpContext.Current.Response;
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "Отчет_133_" + ReportDate.ToShortDateString() + ".xls"));
response.Clear();
response.BinaryWrite(ms.GetBuffer());
fs.Close();
ms.Close();
response.End();
$.ajax({
type: 'GET',
url: 'Get133Report.aspx?date=' + date,
beforeSend: function () {
ShowProgress();
},
complete: function () {
HideProgress();
},
//async: true,
success: function (response) {
//console.log(response);
var blob = new Blob([response], { type: 'application/vnd.ms-excel' });
var downloadUrl = URL.createObjectURL(blob);
var a = document.createElement("a");
a.href = downloadUrl;
a.download = "Отчет_133_"+date+".xls";
document.body.appendChild(a);
a.click();
}
});
Answer the question
In order to leave comments, you need to log in
Solved through using XMLHttpRequest directly
var xhr = new XMLHttpRequest();
xhr.open('GET', 'Get133Report.aspx?date=' + date, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function (e) {
var blob = new Blob([this.response], { type: 'application/vnd.ms-excel' });
var downloadUrl = URL.createObjectURL(blob);
var a = document.createElement("a");
a.href = downloadUrl;
a.download = "Отчет_133_" + date + ".xls";
document.body.appendChild(a);
a.click();
HideProgress();
};
ShowProgress();
xhr.send();
Why is this shamanism? Make location.replace(url_to_file)
https://codepen.io/stalker-red/pen/gdwPRY?editors=1010
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question