FileLoader.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. [page:Loader] &rarr;
  11. <h1>[name]</h1>
  12. <p class="desc">
  13. A low level class for loading resources with XMLHttpRequest, used internaly by most loaders.
  14. It can also be used directly to load any file type that does not have a loader.
  15. </p>
  16. <h2>Code Example</h2>
  17. <code>
  18. const loader = new THREE.FileLoader();
  19. //load a text file and output the result to the console
  20. loader.load(
  21. // resource URL
  22. 'example.txt',
  23. // onLoad callback
  24. function ( data ) {
  25. // output the text to the console
  26. console.log( data )
  27. },
  28. // onProgress callback
  29. function ( xhr ) {
  30. console.log( (xhr.loaded / xhr.total * 100) + '% loaded' );
  31. },
  32. // onError callback
  33. function ( err ) {
  34. console.error( 'An error happened' );
  35. }
  36. );
  37. </code>
  38. <p>
  39. <em>Note:</em> The cache must be enabled using
  40. <code>THREE.Cache.enabled = true;</code>
  41. This is a global property and only needs to be set once to be used by all loaders that use FileLoader internally.
  42. [page:Cache Cache] is a cache module that holds the response from each request made through this loader, so each file is requested once.
  43. </p>
  44. <h2>Constructor</h2>
  45. <h3>[name] ( [param:LoadingManager manager] )</h3>
  46. <p>
  47. [page:LoadingManager manager] — The [page:LoadingManager loadingManager] for the loader to use.
  48. Default is [page:DefaultLoadingManager].
  49. </p>
  50. <h2>Properties</h2>
  51. <p>See the base [page:Loader] class for common properties.</p>
  52. <h3>[property:String mimeType]</h3>
  53. <p>
  54. The expected [link:https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types mimeType].
  55. See [page:.setMimeType]. Default is *undefined*.
  56. </p>
  57. <h3>[property:String responseType]</h3>
  58. <p>The expected response type. See [page:.setResponseType]. Default is *undefined*.</p>
  59. <h2>Methods</h2>
  60. <p>See the base [page:Loader] class for common methods.</p>
  61. <h3>[method:XMLHttpRequest load]( [param:String url], [param:Function onLoad], [param:Function onProgress], [param:Function onError] )</h3>
  62. <p>
  63. [page:String url] — the path or URL to the file. This can also be a
  64. [link:https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs Data URI].<br />
  65. [page:Function onLoad] (optional) — Will be called when loading completes. The argument will be the loaded response.<br />
  66. [page:Function onProgress] (optional) — Will be called while load progresses. The argument will be the XMLHttpRequest instance,
  67. which contains .[page:Integer total] and .[page:Integer loaded] bytes.<br />
  68. [page:Function onError] (optional) — Will be called if an error occurs.<br /><br />
  69. Load the URL and pass the response to the onLoad function.
  70. </p>
  71. <h3>[method:FileLoader setMimeType]( [param:String mimeType] )</h3>
  72. <p>
  73. Set the expected [link:https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types mimeType]
  74. of the file being loaded. Note that in many cases this will be determined automatically, so by default it is *undefined*.
  75. </p>
  76. <h3>[method:FileLoader setResponseType]( [param:String responseType] )</h3>
  77. <p>
  78. Change the response type. Valid values are:<br />
  79. [page:String text] or empty string (default) - returns the data as [page:String String].<br />
  80. [page:String arraybuffer] - loads the data into a [link:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer ArrayBuffer] and returns that.<br />
  81. [page:String blob] - returns the data as a [link:https://developer.mozilla.org/en/docs/Web/API/Blob Blob].<br />
  82. [page:String document] - parses the file using the [link:https://developer.mozilla.org/en-US/docs/Web/API/DOMParser DOMParser].<br />
  83. [page:String json] - parses the file using [link:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse JSON.parse].<br />
  84. </p>
  85. <h2>Source</h2>
  86. <p>
  87. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  88. </p>
  89. </body>
  90. </html>