You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The **FileAttachments** function exported by the standard library is an abstract class that can be used to fetch files by name from remote URLs. To make it concrete, you call it with a *resolve* function, which is an async function that takes a *name* and returns a URL at which the file of that name may be loaded. For example:
286
-
287
-
```js
288
-
constFileAttachment=FileAttachments((name) =>
289
-
`https://my.server/notebooks/demo/${name}`
290
-
);
291
-
```
292
-
293
-
Or, with a more complex example, calling an API to produce temporary URLs:
Once you have your **FileAttachment** function defined, you can call it from notebook code:
285
+
Returns the file attachment with the given *name*, or throws an error if there is no file with the given name.
305
286
306
287
```js
307
288
photo =FileAttachment("sunset.jpg")
308
289
```
309
290
310
291
FileAttachments work similarly to the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch), providing methods that return promises to the file’s contents in a handful of convenient forms.
Returns a promise to a file loaded as an [HTML Image](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image). Note that the promise won't resolve until the image has finished loading — making this a useful value to pass to other cells that need to process the image, or paint it into a `<canvas>`.
319
+
Returns a promise to a file loaded as an [Image](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image). The promise resolves when the image has finished loading, making this useful for reading the image pixels in Canvas, or for loading the image into a WebGL texture. consider [*attachment*.url](#attachment_url) if you want to embed an image in HTML or Markdown.
Returns a promise to the file’s contents as an [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer).
*Note: this function is not part of the Observable standard library (in notebooks), but is provided by this module as a means for defining custom file attachment implementations when working directly with the Observable runtime.*
357
+
358
+
Returns a [*FileAttachment*](#FileAttachment) function given the specified *resolve* function. The *resolve* function is an async function that takes a *name* and returns a URL at which the file of that name can be loaded. For example:
359
+
360
+
```js
361
+
constFileAttachment=FileAttachments((name) =>
362
+
`https://my.server/notebooks/demo/${name}`
363
+
);
364
+
```
365
+
366
+
Or, with a more complex example, calling an API to produce temporary URLs:
0 commit comments