Use case: user captures a series of photos in the field and wants to export them from the app as a single zip to share with someone who cannot access the app. Each photo is a separate object in the database with a file field.
Is it possible to do this via a CloudCode task? It would be fine if user can download via the app on-device or if we have to send out an email with a link. Anyone have a code sample for accomplishing this?
Hi Fletcher
You can do this via CC using a lib like 'jszip'
to create a base64
version of a zip file and then using the base64
data to create an Attachment
in the JourneyApps DB. The attachment field will have to be defined as a media="any"
field. Like so
<field name="zip_file" label="Zip File" type="attachment" media="any" />
Here is an example showing how you can create such a file from CloudCode.
const JSZip = require('jszip');
export async function run() {
// Your code here
let photoObjects = await DB.photoObj.where("photo != ?", null).toArray();
let zip = new JSZip();
// add the images to the zip
for (let i = 0; i < photoObjects.length; i++) {
let photoObject = photoObjects[i];
let photo = photoObject.photo
// Check if photo has been synched to server
if (photo.present()) {
let imgData = await photo.toBase64()
let filename = `Photo_${i+1}.png`
zip.file(filename, imgData, { base64: true });
} else {
// skip this photo or break? your choice
}
}
// create base64 representation of the zip
let zip64 = await zip.generateAsync({ type: "base64" })
// save the zip to the DB on some object
let object = DB.someObj.create();
let newAttachment = await Attachment.create({ filename: 'zipfile.zip', base64: zip64 });
object.zip = newAttachment;
await object.save();
}
I hope this helps.
Please note, you can do a similar thing directly in the app by using HTML Bridge / JourneyApps iFrame to “host” the JSzip lib and then simply passing the zip64
data back to the view from where you can similarly create the Attachment
and save it to the DB.