Uploading Attachments
This text provides an example of how to call Upload attachments interface.
Example: Upload an attachment and bind to AITable
Suppose you have a picture, you want to upload the picture to AITable
Your action steps below:
Get your API Token.(How to get it)
Get the ID of the datasheet.(How to get it)
Get your absolute path of your local picture.
Open the terminal on your computer, execute the following code and send the query request to the server (assuming datasheetId is
dstWUHwzTHd2YQaXEE
, path of local pictures is/Users/coco/Documents/3.jpg
):- cURL
- Javascript SDK
- Python SDK
curl -X POST \
https://aitable.ai/fusion/v1/datasheets/dstWUHwzTHd2YQaXEE/attachments \
-H 'Authorization: Bearer {Your API Token}' \
-H 'content-type: multipart/form-data' \
-F 'file=@/Users/coco/Documents/3.jpg'Note: Need to Download and initialize Javascript SDK first, and then execute the following command.
import { APITable } from 'apitable';
const apitable = new APITable({
token: 'Your API Token',
});
const datasheet = apitable.datasheet("dstWUHwzTHd2YQaXEE");
// in the node environment
const file = fs.createReadStream('/Users/coco/Documents/3.jpg')
try {
const resp = await datasheet.upload(file)
if (resp.success) {
const uploaded_attachments = resp.data
await apitable.datasheet('dstWUHwzTHd2YQaXEE').records.create([{
'title': 'Title A',
'photos': [uploaded_attachments]
}])
}
} catch (error) {
console.error(error)
}Note: You need to download and initialize the Python SDK first, and then execute the following command.
from apitable import Apitable
apitable = Apitable("Your API Token")
dst = apitable.datasheet("dstWUHwzTHd2YQaXEE")
# Upload a file to the specified datasheet
file = dst.upload_file("/Users/coco/Documents/3.jpg")
# Update the "Attachments" field of a specified record
record = dst.records.get(Nickname="Anan")
record.attachment = [file]The server returns the following JSON data, below the
"data"
is all upload successful attachment information:For the meaning of each parameter in the response, please check the API Reference.
{
"code": 200,
"success": true,
"data": {
"token": "space/2021/06/30/d336232203054effb819231a3426d40d",
"mimeType": "image/jpeg",
"size": 229426,
"height": 1024,
"width": 1792,
"name": "3.jpg",
"url": "https://s1.aitable.ai/space/2021/06/30/d336232203054effb819231a3426d40d"
},
"message": "SUCCESS"
}