### Node.js Installation
Source: https://github.com/eligrey/filesaver.js/blob/master/README.md
Installs FileSaver.js using npm or bower for basic Node.js projects.
```bash
# Basic Node.JS installation
npm install file-saver --save
bower install file-saver
```
--------------------------------
### TypeScript Definitions Installation
Source: https://github.com/eligrey/filesaver.js/blob/master/README.md
Installs additional TypeScript definitions for FileSaver.js development.
```bash
# Additional typescript definitions
npm install @types/file-saver --save-dev
```
--------------------------------
### Save Text File
Source: https://github.com/eligrey/filesaver.js/blob/master/README.md
Save a simple text file to the client's download directory. This example creates a Blob object with the text content and then uses saveAs to initiate the download.
```javascript
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "hello world.txt");
```
--------------------------------
### Save Text File using require()
Source: https://github.com/eligrey/filesaver.js/blob/master/README.md
Save a text file using FileSaver.js when using CommonJS module system. Ensure you have the file-saver module installed and required.
```javascript
var FileSaver = require('file-saver');
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "hello world.txt");
```
--------------------------------
### Save Blob from Angular HTTP Client
Source: https://github.com/eligrey/filesaver.js/wiki/Saving-a-remote-file
Download and save files using Angular's HTTP client. This example demonstrates a POST request with specific headers and response type for saving a PDF.
```typescript
import {ResponseContentType } from '@angular/http'
@Injectable()
export class AngularService {
constructor(private http: Http) {}
download(model: MyModel) { //get file from service
this.http.post("http://localhost/a2/pdf.php", JSON.stringify(model), {
method: RequestMethod.Post,
responseType: ResponseContentType.Blob,
headers: new Headers({'Content-Type', 'application/x-www-form-urlencoded'})
}).subscribe(
response => { // download file
var blob = new Blob([response.blob()], {type: 'application/pdf'});
var filename = 'file.pdf';
saveAs(blob, filename);
},
error => {
console.error(`Error: ${error.message}`);
}
);
}
}
```
--------------------------------
### Download File with `a[download]` Attribute
Source: https://github.com/eligrey/filesaver.js/wiki/Saving-a-remote-file
Use the `a[download]` attribute to prompt the browser to download a URL instead of navigating to it. This is useful when you cannot change response headers. The attribute's value serves as the default filename.
```html
download cat.png
```
--------------------------------
### HTTP Headers for File Download
Source: https://github.com/eligrey/filesaver.js/wiki/Saving-a-remote-file
Use `Content-Type`, `Content-Disposition`, and `Content-Length` headers to instruct the browser to download a file. This method offers good cross-browser compatibility and doesn't require JavaScript.
```http
Content-Type: 'application/octet-stream; charset=utf-8'
Content-Disposition: attachment; filename="filename.jpg"; filename*=filename.jpg
Content-Length:
```
--------------------------------
### Save File Constructor
Source: https://github.com/eligrey/filesaver.js/blob/master/README.md
Saves a File constructor instance. Note that IE and Edge do not support the new File constructor, so constructing Blobs and using saveAs(blob, filename) is recommended for broader compatibility.
```javascript
// Note: Ie and Edge don't support the new File constructor,
// so it's better to construct blobs and use saveAs(blob, filename)
var file = new File(["Hello, world!"], "hello world.txt", {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(file);
```
--------------------------------
### Save Blob from Fetch API (ES7, ES6, ES5)
Source: https://github.com/eligrey/filesaver.js/wiki/Saving-a-remote-file
Save files downloaded using the Fetch API. Supports modern (ES7/ES6) and older (ES5) JavaScript syntax. Ensure the response is converted to a blob before saving.
```javascript
// ES7
const res = await fetch(url)
const blob = await res.blob()
saveAs(blob, fileName)
```
```javascript
// ES6
fetch(url)
.then(res => res.blob())
.then(blob => saveAs(blob, fileName))
```
```javascript
// ES5
fetch(url)
.then(function(res) {
return res.blob()
})
.then(function(blob) {
saveAs(blob, fileName)
})
```
--------------------------------
### Basic File Saving with FileSaver.js
Source: https://github.com/eligrey/filesaver.js/wiki/FileSaver.js-Example
This snippet shows how to save a text file using the `saveAs` function from FileSaver.js. It creates a Blob object from the text content and specifies the MIME type and filename. A fallback mechanism is included for browsers that do not support the Blob API.
```javascript
```
--------------------------------
### Feature Detection for FileSaver.js Support
Source: https://github.com/eligrey/filesaver.js/blob/master/README.md
Check if the Blob constructor is supported in the browser to determine FileSaver.js compatibility. This is useful for progressive enhancement.
```javascript
try {
var isFileSaverSupported = !!new Blob;
} catch (e) {}
```
--------------------------------
### Import saveAs from FileSaver.js
Source: https://github.com/eligrey/filesaver.js/blob/master/README.md
Import the saveAs function from the file-saver module using ES module syntax. This is the standard way to include FileSaver.js in modern JavaScript projects.
```javascript
import { saveAs } from 'file-saver';
```
--------------------------------
### Save Blob from Angular 1.x $http
Source: https://github.com/eligrey/filesaver.js/wiki/Saving-a-remote-file
Download and save files using Angular 1.x's $http service. Set responseType to 'blob' and access the blob data from the response.
```javascript
$http({
url: "http://localhost:8080/filename.zip",
responseType: "blob"
}).then(function(response) {
saveAs(response.data, fileName)
})
```
--------------------------------
### Save Blob from XMLHttpRequest
Source: https://github.com/eligrey/filesaver.js/wiki/Saving-a-remote-file
Use XMLHttpRequest to download a file as a blob and save it using FileSaver.js. Ensure responseType is set to 'blob'.
```javascript
var xhr = new XMLHttpRequest()
xhr.open(method, url)
xhr.responseType = 'blob'
xhr.onload = function() {
FileSaver.saveAs(xhr.response, filename);
}
xhr.send()
```
--------------------------------
### Save Canvas Content
Source: https://github.com/eligrey/filesaver.js/blob/master/README.md
Save the content of an HTML canvas element as an image file. This requires the canvas element to be present in the DOM and uses the `toBlob` method.
```javascript
var canvas = document.getElementById("my-canvas");
canvas.toBlob(function(blob) {
saveAs(blob, "pretty image.png");
});
```
--------------------------------
### Save URL Content
Source: https://github.com/eligrey/filesaver.js/blob/master/README.md
Save content from a URL. FileSaver.js handles same-origin URLs using the `a[download]` attribute and cross-origin URLs by attempting a CORS request or falling back to `a[download]`.
```javascript
FileSaver.saveAs("https://httpbin.org/image", "image.jpg");
```
--------------------------------
### saveAs Function
Source: https://github.com/eligrey/filesaver.js/blob/master/README.md
The primary function to save a Blob, File, or URL. It takes the data to be saved and an optional filename. It can also accept an options object for features like automatic BOM.
```APIDOC
## saveAs Function
### Description
Saves a Blob, File, or URL to the client's file system.
### Method Signature
`FileSaver.saveAs(Blob/File/Url, optional DOMString filename, optional Object { autoBom })`
### Parameters
- **Blob/File/Url**: The data to be saved. This can be a `Blob` object, a `File` object, or a URL.
- **filename** (DOMString, optional): The desired name for the saved file.
- **options** (Object, optional): An object that can contain additional options.
- **autoBom** (boolean, optional): If `true`, FileSaver.js will automatically provide Unicode text encoding hints (byte order mark). This is only applied if the blob type has `charset=utf-8` set.
### Examples
#### Saving text using require()
```javascript
var FileSaver = require('file-saver');
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "hello world.txt");
```
#### Saving text
```javascript
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "hello world.txt");
```
#### Saving URLs
```javascript
FileSaver.saveAs("https://httpbin.org/image", "image.jpg");
```
#### Saving a canvas
```javascript
var canvas = document.getElementById("my-canvas");
canvas.toBlob(function(blob) {
saveAs(blob, "pretty image.png");
});
```
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.