|
| 1 | +# File Upload |
| 2 | + |
| 3 | +The following script uploads an image from the local filesystem and attaches it to an image column. |
| 4 | + |
| 5 | +!!! note "seatable-api NPM Package" |
| 6 | + |
| 7 | + The script does **not** use the `seatable-api` NPM package since the package does not currently support file/image uploads. |
| 8 | + Instead, `fetch()` is used. The script does not require any external dependencies. |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +You need a valid API token in order to execute the script. |
| 13 | +Set the `API_TOKEN` variable inside the script to the value of your token. |
| 14 | +You can generate an API token inside the [SeaTable UI](https://seatable.com/help/create-api-tokens/) or by using your [account token](https://api.seatable.com/reference/createapitoken). |
| 15 | + |
| 16 | +## Code |
| 17 | + |
| 18 | +```js |
| 19 | +import { readFileSync } from 'fs'; |
| 20 | +import { basename } from 'path'; |
| 21 | + |
| 22 | +const SERVER_URL = 'https://cloud.seatable.io'; |
| 23 | +const API_TOKEN = ''; |
| 24 | +const TABLE_NAME = 'Table1'; |
| 25 | +const IMAGE_COLUMN_NAME = 'Images'; |
| 26 | + |
| 27 | +// Absolute path to the file on the local filesystem |
| 28 | +const FILE_PATH = 'Test.svg'; |
| 29 | +const FILE_NAME = basename(FILE_PATH); |
| 30 | + |
| 31 | +/** |
| 32 | + * Get file upload link |
| 33 | + * Docs: https://api.seatable.com/reference/getuploadlink |
| 34 | + */ |
| 35 | +let url = `${SERVER_URL}/api/v2.1/dtable/app-upload-link/`; |
| 36 | + |
| 37 | +console.log("Generating upload link...\n"); |
| 38 | + |
| 39 | +let response = await fetch(url, { |
| 40 | + method: "GET", |
| 41 | + headers: { Authorization: `Token ${API_TOKEN}` }, |
| 42 | +}); |
| 43 | + |
| 44 | +const uploadLink = await response.json(); |
| 45 | + |
| 46 | +console.log(uploadLink, '\n'); |
| 47 | + |
| 48 | +/** |
| 49 | + * Upload file from the local filesystem |
| 50 | + * Docs: https://api.seatable.com/reference/uploadfile |
| 51 | + */ |
| 52 | +const file = readFileSync(FILE_PATH); |
| 53 | + |
| 54 | +const formData = new FormData(); |
| 55 | +formData.append("parent_dir", uploadLink.parent_path); |
| 56 | +formData.append("file", new Blob([file.buffer]), FILE_NAME); |
| 57 | +formData.append('relative_path', uploadLink.img_relative_path); |
| 58 | + |
| 59 | +console.log('Uploading file...\n') |
| 60 | + |
| 61 | +response = await fetch(uploadLink.upload_link + "?ret-json=1", { |
| 62 | + method: "POST", |
| 63 | + body: formData, |
| 64 | +}); |
| 65 | + |
| 66 | +const files = await response.json(); |
| 67 | + |
| 68 | +console.log(files, '\n'); |
| 69 | + |
| 70 | +/** |
| 71 | + * Generate base token by using an API token |
| 72 | + * Base operations such as inserting or updating rows require a base token |
| 73 | + * Docs: https://api.seatable.com/reference/getbasetokenwithapitoken |
| 74 | + */ |
| 75 | +url = `${SERVER_URL}/api/v2.1/dtable/app-access-token/`; |
| 76 | + |
| 77 | +console.log('Generating base token...\n'); |
| 78 | + |
| 79 | +response = await fetch(url, { |
| 80 | + headers: { Authorization: `Token ${API_TOKEN}` } |
| 81 | +}); |
| 82 | + |
| 83 | +const baseToken = await response.json(); |
| 84 | + |
| 85 | +console.log(baseToken, '\n'); |
| 86 | + |
| 87 | +/** |
| 88 | + * Append row to base |
| 89 | + * This attaches the image to an image column |
| 90 | + * This API call requires a valid base token |
| 91 | + * Docs: https://api.seatable.com/reference/appendrows |
| 92 | + */ |
| 93 | +const workspaceId = baseToken.workspace_id; |
| 94 | +const baseUuid = baseToken.dtable_uuid; |
| 95 | +const relativeImageURL = `/workspace/${workspaceId}${uploadLink.parent_path}/${uploadLink.img_relative_path}/${files[0].name}`; |
| 96 | + |
| 97 | +const body = { |
| 98 | + table_name: TABLE_NAME, |
| 99 | + rows: [ |
| 100 | + { |
| 101 | + // The values of image/file columns are arrays |
| 102 | + [IMAGE_COLUMN_NAME]: [relativeImageURL], |
| 103 | + }, |
| 104 | + ], |
| 105 | +}; |
| 106 | + |
| 107 | +url = `${SERVER_URL}/api-gateway/api/v2/dtables/${baseUuid}/rows/`; |
| 108 | + |
| 109 | +console.log('Appending row...\n') |
| 110 | + |
| 111 | +response = await fetch(url, { |
| 112 | + method: 'POST', |
| 113 | + headers: { |
| 114 | + accept: 'application/json', |
| 115 | + authorization: `Bearer ${baseToken.access_token}`, |
| 116 | + 'Content-Type': 'application/json', |
| 117 | + }, |
| 118 | + body: JSON.stringify(body), |
| 119 | +}); |
| 120 | + |
| 121 | +console.log(await response.json()) |
| 122 | +``` |
| 123 | + |
| 124 | +## Executing the Script |
| 125 | + |
| 126 | +You can use the following command to execute the script on the commandline using Node.js: |
| 127 | + |
| 128 | +```bash |
| 129 | +node upload-file.js |
| 130 | +``` |
0 commit comments