mirror of https://github.com/nocodb/nocodb
Wing-Kam Wong
1 year ago
5 changed files with 169 additions and 11 deletions
@ -0,0 +1,140 @@
|
||||
--- |
||||
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(); |
||||
})(); |
||||
``` |
Loading…
Reference in new issue