Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
blocks:api:upload [2022-06-27 20:08] admin created |
blocks:api:upload [2025-02-04 13:57] (current) admin [File Upload API] |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== File Upload API ====== | ====== File Upload API ====== | ||
- | Blocks provides an HTTP API for uploading files, for example images. Uploaded files are temporarily stored in a subdirectory of the //temp// directory inside your Blocks root directory, | + | Blocks provides an HTTP API for uploading files, for example images. Uploaded files are temporarily stored in a subdirectory of the //temp// directory inside your Blocks root directory. This directory is not externally accessible for reading data. Thus, uploaded files must then be moved or further processed by a script. As an example, [[blocks: |
+ | :!: **IMPORTANT**: | ||
===== Upload file using multipart/ | ===== Upload file using multipart/ | ||
**URL:** / | **URL:** / | ||
Line 23: | Line 24: | ||
</ | </ | ||
- | ===== Upload | + | ===== Upload |
**URL:** / | **URL:** / | ||
Line 32: | Line 33: | ||
The server returns the path inside the //temp// directory where the file was stored or, in case of an error, an error message along with status code indiating an error. | The server returns the path inside the //temp// directory where the file was stored or, in case of an error, an error message along with status code indiating an error. | ||
- | Here's is a JavaScript example showing how to upload a text file with a unique file name using this method. The promise returned by the fetch call will eventually provide the path to the file if the upload was successful. | + | Here's is a JavaScript example showing how to upload a short text snippet (specified by the body) to a file with a unique file name using this method. The promise returned by the fetch call will eventually provide the path to the file if the upload was successful. |
< | < | ||
Line 44: | Line 45: | ||
</ | </ | ||
+ | ===== Upload an image as urlencoded data ===== | ||
+ | |||
+ | Here's is an example showing how to upload a PNG image with a unique file name as urlencoded data. Once this is done, the script logs the server-side name of the file, which you would then normally use to call some custom [[blocks: | ||
+ | |||
+ | < | ||
+ | <canvas id=" | ||
+ | <button id=" | ||
+ | |||
+ | < | ||
+ | const canvas = document.getElementById(" | ||
+ | const ctx = canvas.getContext(" | ||
+ | |||
+ | // Draw something on the canvas. | ||
+ | ctx.fillStyle = " | ||
+ | ctx.arc(320, | ||
+ | ctx.fill(); | ||
+ | |||
+ | document.getElementById(" | ||
+ | | ||
+ | // Convert canvas content to Blob (containing PNG data). | ||
+ | canvas.toBlob(async function(blob) { | ||
+ | | ||
+ | // Upload image as PNG. | ||
+ | let res = await fetch("/ | ||
+ | method: " | ||
+ | headers: { | ||
+ | " | ||
+ | }, | ||
+ | body: blob | ||
+ | }); | ||
+ | |||
+ | // Retrieve file name from response and log it in the console. | ||
+ | let fileName = await res.text(); | ||
+ | console.log(fileName); | ||
+ | }); | ||
+ | }); | ||
+ | </ | ||
+ | </ |