Skip to content

Instantly share code, notes, and snippets.

View jasonweng's full-sized avatar

jason jasonweng

  • The Plant
  • Hangzhou China
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]*)/;