|
| 1 | +# File upload |
| 2 | + |
| 3 | +## Basics |
| 4 | + |
| 5 | +You can customize the properties of the file upload components in the right panel, such as the displayed text, file types, upload type. |
| 6 | + |
| 7 | +<figure><img src="../../.gitbook/assets/file-upload-1.PNG" alt=""><figcaption></figcaption></figure> |
| 8 | + |
| 9 | +### File type |
| 10 | + |
| 11 | +You can input an array of strings to restrict the types of the files to be uploaded. The default value of file type is empty, meaning that no limitation is pre-defined. Each string value in a specified file type array should be a [unique file type specifier](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#unique\_file\_type\_specifiers) in one of the following formats. |
| 12 | + |
| 13 | +* A valid case-insensitive filename extension, starting with a period character ("."), such as `.png`, `.txt`, and `.pdf`. |
| 14 | +* A valid string in [MIME format](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics\_of\_HTTP/MIME\_types) without an extension. |
| 15 | +* String `audio/*` indicating "any audio file". |
| 16 | +* String `video/*` indicating "any video file". |
| 17 | +* String `image/*` indicating "any image file". |
| 18 | + |
| 19 | +For example, when the value of file type is `[".pdf", ".mp4", "image/*"]`, you can upload PDF files, MP4 files, and any type of image files. |
| 20 | + |
| 21 | +### Upload type |
| 22 | + |
| 23 | +You can decide whether to upload a single file, multiple files, or a directory. |
| 24 | + |
| 25 | +<figure><img src="../../.gitbook/assets/file-upload-2.PNG" alt=""><figcaption></figcaption></figure> |
| 26 | + |
| 27 | +### Display uploaded files |
| 28 | + |
| 29 | +Switch on or off **Show upload list** to display or hide the list of the uploaded files. You can also set this property via JS code. By default, its value is "true". |
| 30 | + |
| 31 | +The upload list presents the file names of all uploaded files in chronological order. You can also access the name of the uploaded files via the property `files[index].name`. When hovering your mouse over a file, the 🗑️ icon appears and you can click it to delete the corresponding file. |
| 32 | + |
| 33 | +<figure><img src="../../.gitbook/assets/file-upload-3.png" alt=""><figcaption></figcaption></figure> |
| 34 | + |
| 35 | +### Parse files |
| 36 | + |
| 37 | +Toggle **Parse files** and Openblocks will try to parse the uploaded file data structure into objects, arrays, or strings. You can access the parsed result via the property `parsedValue`. Openblocks supports parsing Excel, JSON, and CSV files. The parsing result of other types of files is `null`. |
| 38 | + |
| 39 | +<figure><img src="../../.gitbook/assets/file-upload-4.PNG" alt=""><figcaption></figcaption></figure> |
| 40 | + |
| 41 | +## Validation |
| 42 | + |
| 43 | +Under the validation tab, you can configure how many files are allowed to be uploaded, as well as the minimum and maximum size of a single file to be uploaded. |
| 44 | + |
| 45 | +<figure><img src="../../.gitbook/assets/file-upload-5.PNG" alt=""><figcaption></figcaption></figure> |
| 46 | + |
| 47 | +### Max files |
| 48 | + |
| 49 | +When the upload type is "Multiple" or "Directory", you can set **Max files** to limit the maximum number of files to upload. If the number of files to be uploaded exceeds this threshold, the latest uploaded files will replace the oldest ones. |
| 50 | + |
| 51 | +### File size |
| 52 | + |
| 53 | +You can set the minimum and maximum size of the files to upload, using KB, MB, GB, or TB units. The default unit for file size is byte.When the size of the uploaded file exceeds the limit, you will see a global alert. |
| 54 | + |
| 55 | +<figure><img src="../../.gitbook/assets/file-upload-6.png" alt=""><figcaption></figcaption></figure> |
| 56 | + |
| 57 | +### Access uploaded files |
| 58 | + |
| 59 | +Files uploaded via the file upload component are stored in browser cache memory in **base64-encoded** string format. To store these files in data sources, you need to build queries to connect to databases or APIs.You can view the properties of the uploaded files in the data browser in the left pane, or access property names in `{{}}` or JS queries via JS code. Commonly used properties are as follows. |
| 60 | + |
| 61 | +* `value`: A list of the content of the uploaded files, encoded in base64. |
| 62 | +* `files`: A list of metadata of the uploaded files, including `uid`, `name`, `type`, `size`, and `lastModified`. |
| 63 | +* `parsedValue`: A list of the value of the parsed files. |
| 64 | + |
| 65 | +<figure><img src="../../.gitbook/assets/file-upload-7.png" alt=""><figcaption></figcaption></figure> |
| 66 | + |
| 67 | +## Use case: Upload images to Postman Echo API |
| 68 | + |
| 69 | +This example uploads images to Postman [Echo API](https://learning.postman.com/docs/developer/echo-api/), and inspects the query result whether the files are sent successfully to the remote server. For more details, see [Image uploader](https://cloud.openblocks.dev/apps/63a2651e660ad97d59eb4a51/view). |
| 70 | + |
| 71 | +<figure><img src="../../.gitbook/assets/file-upload-8.gif" alt=""><figcaption></figcaption></figure> |
| 72 | + |
| 73 | +### Upload a single file |
| 74 | + |
| 75 | +1. Insert **File upload** component `file1`, and set **File types** and **Upload type**.  |
| 76 | + |
| 77 | + <figure><img src="../../.gitbook/assets/file-upload-9.png" alt=""><figcaption></figcaption></figure> |
| 78 | +2. Insert an **Image** component, and set the image source as `data:image;base64,{{file1.value[0]}}` to preview the image cached in the browser.  |
| 79 | + |
| 80 | + <figure><img src="../../.gitbook/assets/file-upload-10.png" alt=""><figcaption></figcaption></figure> |
| 81 | +3. Create a new query `uploadSingle` to send single images to Echo API. You can add a button, and bind its click event to the query. Select "POST" as the **HTTP Method**, and complete the **Body** in format **** `{data: base64 string, name: string}`, for example–`{data: {{file1.value[0]}}, name: {{file1.files[0].name}}}`.  |
| 82 | + |
| 83 | + <figure><img src="../../.gitbook/assets/file-upload-11.png" alt=""><figcaption></figcaption></figure> |
| 84 | +4. Run query `uploadSingle`. Create a new text component to display the file name of the returned value of the query by `{{Object.keys(uploadSingle.data?.files??[])[0]}}`. Add a new image component to view the returned image by `{{Object.values(uploadSingle.data?.files??[])[0]}}`.  |
| 85 | + |
| 86 | + <figure><img src="../../.gitbook/assets/file-upload-12.png" alt=""><figcaption></figcaption></figure> |
| 87 | + |
| 88 | +### Upload multiple files |
| 89 | + |
| 90 | +When uploading multiple files, you send an object of array to Echo API. Each item in the array should use the format `{data: base64 string, name: string}`. You can create a transformer `multipleFileArray` to convert all files to be uploaded to the target format, as shown in the following figure. |
| 91 | + |
| 92 | +<figure><img src="../../.gitbook/assets/file-upload-13.png" alt=""><figcaption></figcaption></figure> |
| 93 | + |
| 94 | +Then, create a new query `uploadMultiple` to upload the images to Echo API. In **Body** field, you can use the returned value of query `multipleFileArray` directly. |
| 95 | + |
| 96 | +<figure><img src="../../.gitbook/assets/file-upload-14.png" alt=""><figcaption></figcaption></figure> |
| 97 | + |
| 98 | +Run this query and check the result. The file names and the encoded content are properly accessed. |
| 99 | + |
| 100 | +<figure><img src="../../.gitbook/assets/file-upload-15.png" alt=""><figcaption></figcaption></figure> |
| 101 | + |
| 102 | +You can present the query result using a [**List View**](list-view.md) **** component. |
| 103 | + |
| 104 | +* Access the number of rows by the value of the field`files` in the query result: `{{Object.keys(uploadMultiple.data?.files?? []).length}}`. |
| 105 | +* Access the file names by `{{Object.keys(uploadMultiple.data.files)[i]}}`. |
| 106 | +* Access the file content by `{{Object.values(uploadMultiple.data.files)[i]}}`. |
| 107 | + |
| 108 | +<figure><img src="../../.gitbook/assets/file-upload-16.png" alt=""><figcaption></figcaption></figure> |
0 commit comments