diff --git a/README.md b/README.md index 8cf55b0..901d87e 100644 --- a/README.md +++ b/README.md @@ -60,10 +60,13 @@ import { saveAs } from 'file-saver'; ``` ```js -FileSaver saveAs(Blob/File/Url, optional DOMString filename, optional Object { autoBom }) +FileSaver saveAs(Blob/File/Url, optional DOMString filename, optional Object { autoBom, headers, postData }) ``` +Optional Object: -Pass `{ autoBom: true }` if you want FileSaver.js to automatically provide Unicode text encoding hints (see: [byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark)). Note that this is only done if your blob type has `charset=utf-8` set. +- Pass `{ autoBom: true }` if you want FileSaver.js to automatically provide Unicode text encoding hints (see: [byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark)). Note that this is only done if your blob type has `charset=utf-8` set. +- Pass `{ headers: object }` if you want append headers when fetch data from remote url. +- Pass `{ postData: object }` if you want send data when post data from remote url. Examples -------- diff --git a/src/FileSaver.js b/src/FileSaver.js index 5d204ae..73b9911 100644 --- a/src/FileSaver.js +++ b/src/FileSaver.js @@ -33,7 +33,6 @@ function bom (blob, opts) { function download (url, name, opts) { var xhr = new XMLHttpRequest() - xhr.open('GET', url) xhr.responseType = 'blob' xhr.onload = function () { saveAs(xhr.response, name, opts) @@ -41,7 +40,18 @@ function download (url, name, opts) { xhr.onerror = function () { console.error('could not download file') } - xhr.send() + if (opts && opts.headers) { + for (var key in opts.headers) { + xhr.setRequestHeader(key, opts.headers[key]) + } + } + if (opts && opts.postData) { + xhr.open('POST', url) + xhr.send(opts.postData) + } else { + xhr.open('GET', url) + xhr.send() + } } function corsEnabled (url) {