<a id="downloadlink" onClick="return DownloadExcel()">Download Excel File</a>
//download excel func
function DownloadExcel( file, filename) {
//debugger;
var blob = base64toBlob(file, "data:application/vnd.ms-excel;")
if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {
return navigator.msSaveOrOpenBlob(blob, filename);
}
else{
console.log("inside else of downloadexcel");
var objectUrl = URL.createObjectURL(blob);
var downloadLink = $("#downloadlink");
downloadLink.href = objectUrl; //uri;
downloadLink.download = filename;
// document.body.appendChild(downloadLink);
$(document).append(downloadLink);
}
}
function base64toBlob(b64Data, contentType, sliceSize) {
contentType = contentType || '';
sliceSize = sliceSize || 512;
var byteCharacters = atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {
type: "application/vnd.ms-excel"
});
return blob;
}
// /download excel func
$(document).ready(function(){
$.ajax({
url: "https://myserver.com/files/file.cfc?fileid=12",
type: "GET",
dataType: "json",
cache: false,
success: function(response){
if(response.length != 0){
console.log("Successful file retrieval!");
DownloadExcel( response[0].filecontent, response[0].filename );
}
else{
console.log("Error in file retrieval!");
console.log( response );
}
},
error: function( response ){
console.log("There was an API server error!");
console.log( response );
}
});
});
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE