Following the previous blog of uploading file to VBCS, this time, we handle the download issue.
We can fetch file contents from external web service – etc, fetch a pdf binary string from DB or other report services, or manually construct a csv file content, and put to a VBCS page variable.
OK, and then, how can we download the file from the variable to local PC?
You can add the following function code to page function of VBCS, and hook it up in action chain, pass in the variable with file content, it will directly download the file in your browser.
Please remember to change the file type to the one you required, following code is generating a pdf file
Cheers!
// following is downloading a pdf,
// you can download other files, you just need to change the file type
PageModule.prototype.downloadFile = function(fileBytes){
var blob = new Blob([fileBytes],{type:'application/x-pdf'});
var filename = "test.pdf";
if(navigator.msSaveBlob){ // IE 10+
navigator.msSaveBlob(blob,filename);
} else {
var link = document.createElement("a");
if(link.download !== undefined){ // feature detection
// Browsers that support HTML5 download attribute
var url = URL.createObjectURL(blob);
link.setAttribute("href",url);
link.setAttribute("download",filename);
link.style.visibility='hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
};