Skip to content

Instantly share code, notes, and snippets.

View hungtamse's full-sized avatar

hungtamse

  • Can Tho University
  • Can Tho
View GitHub Profile
@jasonweng
jasonweng / ajax-file-download.js
Created April 21, 2017 08:13
handle file download from AJAX POST
// XMLHttpRequest
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
if (this.status === 200) {
var filename = "";
var disposition = xhr.getResponseHeader('Content-Disposition');
if (disposition && disposition.indexOf('attachment') !== -1) {
var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;