Skip to main content

Loader

FileLoader

A low level class for loading resources with Fetch, used internally by most loaders. It can also be used directly to load any file type that does not have a loader.

Code Example

const loader = new THREE.FileLoader(); //load a text file and output the
result to the console loader.load( // resource URL 'example.txt', // onLoad
callback function ( data ) { // output the text to the console console.log(
data ) }, // onProgress callback function ( xhr ) { console.log( (xhr.loaded /
xhr.total * 100) + '% loaded' ); }, // onError callback function ( err ) {
console.error( 'An error happened' ); } );

Note: The cache must be enabled using

THREE.Cache.enabled = true;  

This is a global property and only needs to be set once to be used by all loaders that use FileLoader internally. Cache is a cache module that holds the response from each request made through this loader, so each file is requested once.

Constructor

FileLoader ( [param:LoadingManager manager] )

manager — The loadingManager for the loader to use. Default is DefaultLoadingManager.

Properties

See the base Loader class for common properties.

mimeType

mimeType: String;  

The expected mimeType. See .setMimeType. Default is undefined.

responseType

responseType: String;  

The expected response type. See .setResponseType. Default is undefined.

Methods

See the base Loader class for common methods.

load

function load( url: String, onLoad: Function, onProgress: Function, onError:
Function ): undefined;

url — the path or URL to the file. This can also be a Data URI.
onLoad (optional) — Will be called when loading completes. The argument will be the loaded response.
onProgress (optional) — Will be called while load progresses. The argument will be the ProgressEvent instance, which contains .lengthComputable, .total and .loaded. If the server does not set the Content-Length header; .total will be 0.
onError (optional) — Will be called if an error occurs.

Load the URL and pass the response to the onLoad function.

setMimeType

function setMimeType( mimeType: String ): this;  

Set the expected mimeType of the file being loaded. Note that in many cases this will be determined automatically, so by default it is undefined.

setResponseType

function setResponseType( responseType: String ): this;  

Change the response type. Valid values are:
text or empty string (default) - returns the data as String.
arraybuffer - loads the data into a ArrayBuffer and returns that.
blob - returns the data as a Blob.
document - parses the file using the DOMParser.
json - parses the file using JSON.parse.

Source

src/loaders/FileLoader.js