Hi,
I am using below code to try to upload an image.I have to do so by iterating over a folder.
onst env = “development” // “staging” , “production”,“development(varies from user to user)”,“testing”
const DB = DB;
let data = ‘images\taran.png’;
let buff = new Buffer(data);
console.log(‘buffer’, buff);
const photoData = Buffer.from([buff]);
console.log( buff.toString(‘base64’));
console.log(‘photoData’, photoData);
let base64data = buff.toString(‘base64’);
let batch = new DB.Batch();
var image= DB.xyz.create();
image.week_number = 1;
image.language = ‘en_US’;
image.image = await Attachment.create({ filename: ‘taran.png’, data: photoData });
await xyz.save();
batch.save(xyz);
await batch.execute();
steven
2
Hi,
Welcome to the forum.
If the files are stored on a local machine, the easiest method is to use local development.
Local Development is explained in these docs.
You would need to initialize the DB using backend API credentials, which are obtainable from the DataBrowser.
The files could be read using the NodeJS fs API.
As a side tip: Local development and running/debugging scripts is made easier and explained by this VSCode Extension.
willem
3
Hi Taranbir,
Expanding on what Steven posted.
Please see example below of how to create a backend object with an image from your local directory by using the JourneyApps backend API.
See Photo field representation here
const image = fs.readFileSync("./image_file.png");
// Photo field representation
const imageAttachment = {
'base64': image.toString("base64"),
'filename': "image_file.png",
};
const payload = {
xyz: {
week_number: 1,
language: ‘en_US’,
image: imageAttachment
},
};
const baseUrl = "https://run-us.journeyapps.com/api/v4/BACKEND_INSTANCE_ID";
const token = "TOKEN FROM BACKEND";
const model = "xyz";
// Create object on the backend
const _response = await fetch(baseUrl + "/objects/" + model + ".json", {
headers: {
Authorization: "Token " + token,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify(payload),
});
if (_response.ok) {
const _json = await _response.json();
console.log("Upload successful", _json);
return _json;
} else {
console.log("Unexpected error", _response);
console.log("Body: " + JSON.stringify(await _response.text()));
return null;
}