Download Multiple Documents

0
Hello All   I have a requirement in which I am supposed to download multiple documents on click of a button. I have tried using ZIP module but that is not storing files inside, instead is giving me just a zip file without any content in it. Would like to know some suggestions on this. Thanks in advance
asked
4 answers
2

Hi Abhishek,

 

I have created a JavaScript action that will download the files as actual files:

// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
import "mx-global";
import { Big } from "big.js";

// BEGIN EXTRA CODE
export async function pause(msec) {
    return new Promise(resolve => {
            setTimeout(resolve, msec || 1000);
        }
    );
}

export function downloadFile(fileDoc) {
	if (fileDoc.get("HasContents")) {
		// get file location on server
		const url = "/file?guid=" + fileDoc.getGuid() + "&target=internal";
		const name = fileDoc.get("Name");
		const message = "file with url:" + url + " and name " + name;

		// console.debug("found " + message);
		// create a dummy link and append to page.
		// trigger click on link to download document
		// remove link again
		const a = document.createElement('a');
		a.href = url;
		a.target = '_parent';
		// use a.download if available, it prevents plugins from opening
		if ('download' in a) {
			a.download = name;
			// Add a to the doc for click to work.
			(document.body || document.documentElement).appendChild(a);

			if (a.click) {
				// console.debug("starting download "  + message);
				a.click(); // trigger a click to initiate the download
			}
			// delete the temporary link.
			a.parentNode.removeChild(a);
		}
	}	
}
export function JS_BatchFileDownload (fileDocumentList, batchSize, delay, index){
	let countToBatchSize = 0;
	let count = index + 1;
	// filter out all fileDoc not downloaded yet
	const fileDocListNoDLYet = fileDocumentList.filter((fileDoc) => {
		return !fileDoc.downloaded;
	})
	if (fileDocListNoDLYet.length && fileDocListNoDLYet.length > 0) {
		console.debug("starting new batch document download at "  + count + " documents.");	
		fileDocListNoDLYet.forEach(	
			async (fileDoc) => {
				count = index + 1;
				if (!fileDoc.downloaded) {
					if (countToBatchSize <= batchSize) {	
						console.debug("starting processing document "  + count);
						if (countToBatchSize < batchSize){
							const name = fileDoc.get("Name");
							console.debug("starting download document "  + count + " with name " + name);
							fileDoc.downloaded = true;
							index++;
							countToBatchSize++;
							downloadFile(fileDoc);
						} else if (countToBatchSize == batchSize) {
							countToBatchSize++;
							await pause(delay).then(() => {
								// console.debug("starting download document "  + count) + " after wait time";
								JS_BatchFileDownload(fileDocumentList, batchSize, delay, index);
								return;
							}).catch(e =>{console.debug(e)});		
						} 		
					}
				}
			}
		)
	}
}
// END EXTRA CODE

/**
 * A JavaScript action which facilitates a multiple file download. It needs a FileDocument list as input parameter and downloads the files. 
 * 
 * The code creates a link for every file which needs to be downloaded, automatically clicks the linking and this initiates the download of the file in the browser. After downloading the link is removed again from the DOM. 
 * 
 * Downloading is done in batches of 5 with a delay between each batch, since browsers (Chrome) tend to not download more than 10 files in one go.
 * 
 * For Internet Explorer a delay of 500 ms is added as that is needed for it to work in IE.
 * @param {MxObject[]} fileDocumentList
 * @returns {Promise.<void>}
 */
export async function JS_MultipleFileDownload(fileDocumentList) {
	// BEGIN USER CODE
	// standards for all browsers but Internet Explorer
	let batchSize = 5,
	delay = 1000,
	index = 0;

	// for Internet Explorer, keep batchSize of one, since it needs a delay for every file.
	// also lower wait time to 500 seconds
	if (window.document.documentMode) {
		batchSize = 1;
		delay = 500;
	}	
	JS_BatchFileDownload(fileDocumentList, batchSize, delay, index);
	// END USER CODE
}

 

answered
0

Hi Abhishek,

 

You are on the right track. Using the Zip Handling module is the right way to download multiple files at once. Maybe something is missing in your implementation, can you share the microflow that creates the zip document?

answered
0

I think you should put the parameter returned from the java action in the download action. Instead you are providing it with the download attach.

answered
0

Hi Abhishek,

 

I think you have to download the created zipfile, like this screenshot

answered