多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.2 KiB

docs: dev resources (#8588) * docs: dev resources * Update packages/noco-docs/docs/160.developer-resources/010.rest-APIs/010.accessing-apis.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update packages/noco-docs/docs/160.developer-resources/010.rest-APIs/010.accessing-apis.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update packages/noco-docs/docs/160.developer-resources/010.rest-APIs/010.accessing-apis.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update packages/noco-docs/docs/160.developer-resources/020.webhook/040.actions-on-webhook.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update packages/noco-docs/docs/160.developer-resources/020.webhook/020.create-webhook.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update packages/noco-docs/docs/160.developer-resources/020.webhook/020.create-webhook.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update packages/noco-docs/docs/160.developer-resources/010.rest-APIs/010.accessing-apis.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update packages/noco-docs/docs/160.developer-resources/020.webhook/020.create-webhook.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update packages/noco-docs/docs/160.developer-resources/020.webhook/040.actions-on-webhook.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update packages/noco-docs/docs/160.developer-resources/010.rest-APIs/010.accessing-apis.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update packages/noco-docs/docs/160.developer-resources/010.rest-APIs/050.upload-via-api.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update packages/noco-docs/docs/160.developer-resources/010.rest-APIs/005.overview.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update packages/noco-docs/docs/160.developer-resources/010.rest-APIs/005.overview.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update packages/noco-docs/docs/160.developer-resources/010.rest-APIs/005.overview.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update packages/noco-docs/docs/160.developer-resources/020.webhook/040.actions-on-webhook.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update packages/noco-docs/docs/160.developer-resources/020.webhook/020.create-webhook.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fix: review comments Signed-off-by: Raju Udava <86527202+dstala@users.noreply.github.com> --------- Signed-off-by: Raju Udava <86527202+dstala@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
7 months ago
---
title: 'Upload via API'
description: 'Upload files locally present or from public remote URL via API'
hide_table_of_contents: true
---
Sample code to upload files via API is listed below.
Assumes `http://localhost:8080/` as the base URL for the API calls.
# Upload local file
```javascript
let axios = require("axios").default;
let FormData = require('form-data');
let fs = require('fs');
// Configurations
//
const project_id = '<Project Identifier>';
const table_id = '<Table Identifier>';
const xc_token = '<Auth Token>';
const file_path = '<Local File Path>';
// Insert Image
// @param image_path : local file path
// @return : JSON object to be used in insert record API for attachment field
//
async function insertImage (path) {
const formData = new FormData();
formData.append("file", fs.createReadStream(path));
const data = await axios({
url: 'http://localhost:8080/api/v2/storage/upload',
data: formData,
headers:{
'Content-Type':`multipart/form-data;`,
'xc-token': xc_token
},
method: 'post',
// Optional : storage file path
params: {"path": "somePath"}
});
return data;
}
// Insert record with attachment
// Assumes a table with two columns :
// 'Title' of type SingleLineText and
// 'Attachment' of type Attachment
//
async function uploadFileExample() {
let response = await insertImage(file_path);
let row = {
"Title": "2",
"Attachment": response.data
};
await axios({
method: 'POST',
url: `http://localhost:8080/api/v2/tables/${table_id}/records`,
data: row,
headers: {
'xc-token': xc_token
}
});
}
(async () => {
await uploadFileExample();
})();
```
# Upload via URL
```javascript
let axios = require("axios").default;
let FormData = require('form-data');
let fs = require('fs');
// Configurations
//
const project_id = '<Project Identifier>';
const table_id = '<Table Identifier>';
const xc_token = '<Auth Token>';
// URL array : URLs of files to be uploaded
const URLs = [{ url: '<URL1>' }, { url: '<URL2>' }];
// Insert Image
// @param URLs : [] containing public URL for files to be uploaded
// @return : JSON object to be used in insert record API for attachment field
//
async function insertImageByURL (URL_array) {
const data = await axios({
url: 'http://localhost:8080/api/v2/storage/upload-by-url',
data: URL_array,
headers: {
'xc-token': xc_token
},
method: 'post',
// Optional : storage file path
params: {"path": "somePath"}
});
return data;
}
// Insert record with attachment
// Assumes a table with two columns :
// 'Title' of type SingleLineText and
// 'Attachment' of type Attachment
//
async function uploadByUrlExample() {
let response = await insertImageByURL(URLs);
// Update two columns : Title and Attachment
let row = {
"Title": "3",
"Attachment": response.data
};
await axios({
method: 'POST',
url: `http://localhost:8080/api/v2/tables/${table_id}/records`,
data: row,
headers: {
'xc-token': xc_auth
}
});
}
(async () => {
await uploadByUrlExample();
})();
```