Question:
How can I allow users to download the data from a multi-document field using a button.
Product:
Wix Studio Editor
What are you trying to achieve:
I have a multi-document field as a part of one of my collections and I would like for users of my site to be able to download this data with the click of a button (or several clicks).
What have you already tried:
I’ve tried doing this using a repeater, a check list to select which files to download and a single button with a link to download all of the files in the field. Here is the code I’ve most recently been trying to get to work:
import wixData from 'wix-data';
$w.onReady(function () {
// Query your collection to get the list of items with multi-document fields
wixData.query("Feedback") // Replace with your collection name
.find()
.then((results) => {
// Assuming the repeater is connected to the items in the collection
$w("#repeater4").data = results.items;
console.error(results);
})
.catch((err) => {
console.error(err);
});
});
// This function runs for each item in the repeater
$w("#repeater4").onItemReady(($item, itemData, index) => {
const docs = itemData.files; // Replace with your actual multi-document field key
if (docs && docs.length > 0) {
// If there are documents in the multi-document field, create a dynamic repeater for them
$item("#docsRepeater").data = docs.map(doc => {
return { fileName: doc.name, fileUrl: doc.fileUrl };
});
// For each document in the inner repeater
$item("#docsRepeater").onItemReady(($docItem, docData, docIndex) => {
$docItem("#text5").text = docData.fileName; // Display document name
$docItem("#button75").link = docData.fileUrl; // Set the button to download the document
console.error();
});
}
});