Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ajax headers and post data #755

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand Down
14 changes: 12 additions & 2 deletions src/FileSaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,25 @@ 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)
}
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) {
Expand Down