mirror of https://github.com/nocodb/nocodb
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.
140 lines
3.3 KiB
140 lines
3.3 KiB
1 year ago
|
---
|
||
|
title: 'Upload via API'
|
||
|
description: 'Upload files locally present or from public remote URL via API'
|
||
|
position: 1600
|
||
|
category: 'Developer Resources'
|
||
|
menuTitle: 'Upload via API'
|
||
|
---
|
||
|
|
||
|
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
|
||
|
|
||
|
```
|
||
|
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/v1/db/storage/upload',
|
||
|
data: formData,
|
||
|
headers:{
|
||
|
'Content-Type':`multipart/form-data;`,
|
||
|
'xc-auth': 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/v1/db/data/noco/${project_id}/${table_id}`,
|
||
|
data: row,
|
||
|
headers: {
|
||
|
'xc-auth': xc_token
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
(async () => {
|
||
|
await uploadFileExample();
|
||
|
})();
|
||
|
```
|
||
|
|
||
|
|
||
|
# Upload via URL
|
||
|
|
||
|
```
|
||
|
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/v1/db/storage/upload-by-url',
|
||
|
data: URL_array,
|
||
|
headers: {
|
||
|
'xc-auth': 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/v1/db/data/noco/${project_id}/${table_id}`,
|
||
|
data: row,
|
||
|
headers: {
|
||
|
'xc-auth': xc_auth
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
(async () => {
|
||
|
await uploadByUrlExample();
|
||
|
})();
|
||
|
```
|