Skip to content

Instantly share code, notes, and snippets.

View AbigaelCodes's full-sized avatar
🪁
Relaxing

Abigael AbigaelCodes

🪁
Relaxing
View GitHub Profile
@AbigaelCodes
AbigaelCodes / ajax-file-download.js
Last active February 18, 2022 16:51 — forked from jasonweng/ajax-file-download.js
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]*)/;