From 3ce0907dfb65e481ee8768d33b6520e2b78e3774 Mon Sep 17 00:00:00 2001 From: lily <1186233735@qq.com> Date: Sat, 9 Jan 2021 19:18:37 +0800 Subject: [PATCH] if name is undefined, get the name of Content-Disposition header instead of download --- src/FileSaver.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/FileSaver.js b/src/FileSaver.js index 5d204ae..b4f8c9b 100644 --- a/src/FileSaver.js +++ b/src/FileSaver.js @@ -36,6 +36,10 @@ function download (url, name, opts) { xhr.open('GET', url) xhr.responseType = 'blob' xhr.onload = function () { + var matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(xhr.getResponseHeader('Content-Disposition')) + if (matches != null && matches[1]) { + name = name || matches[1].replace(/['"]/g, '') + } saveAs(xhr.response, name, opts) } xhr.onerror = function () {