Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hiddenkirby/19e93a69f619ef06621fa0ca4d0fe553 to your computer and use it in GitHub Desktop.
Save hiddenkirby/19e93a69f619ef06621fa0ca4d0fe553 to your computer and use it in GitHub Desktop.
Cross-client solution to download a base64 file for the user.
name: Download a Base64 file.
description: Cross-client solution to download a base64 file for the user.
host: OUTLOOK
api_set: {}
script:
content: |
const test_base64_text_file = "VGhpcyBpcyBhIHRlc3QgdGV4dCBmaWxlLgo=";
const newFileName = "test_download_image.txt";
$("#run").click(run);
$("#download1").click(downloadTempFile1);
$("#download2").click(downloadTempFile2);
$("#download3").click(downloadTempFile3);
function run() {
const userProfile = Office.context.mailbox.userProfile;
Office.context.ui.openBrowserWindow("http://www.office.com");
}
function b64toBlob(b64Data, contentType = "", sliceSize = 512) {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, { type: contentType });
return blob;
}
function downloadTempFile1() {
const a = document.createElement("a"); // Create <a>
a.target = "_blank"; // open in new tab
// need special handling of certain content types?
a.href = "data:octet-stream;base64," + test_base64_text_file; // Base64 Goes here
a.download = newFileName; //File name Here
a.click(); //Download file
}
function downloadTempFile2() {
window.open("data:application/octet-stream;base64," + test_base64_text_file);
}
function downloadTempFile3() {
const blob = b64toBlob(test_base64_text_file, "octet-stream");
const blobUrl = URL.createObjectURL(blob);
const link = document.createElement("a");
link.download = "test_download_file.txt";
link.target = "_blank";
link.href = blobUrl;
link.click();
}
language: typescript
template:
content: >-
<p>This opens a window.</p>
<button id="run" class="ms-Button">
<span class="ms-Button-label">Run</span>
</button>
<p>This uses simple anchor tag and click.</p>
<p>Can name file. Works in Add-in on Web, not Desktop Client</p>
<button id="download1" class="ms-Button">
<span class="ms-Button-label">Run</span>
</button>
<p>This uses window.open to download a file.</p>
<p>Cannot name file. Works in Web. Will open a window in desktop, but not
download a file.</p>
<button id="download2" class="ms-Button">
<span class="ms-Button-label">Run</span>
</button>
<p>This uses blobUrl and simple anchor tag to click.</p>
<p>Can name file. Works in Web, not Desktop Client.</p>
<button id="download3" class="ms-Button">
<span class="ms-Button-label">Run</span>
</button>
language: html
style:
content: |-
section.samples {
margin-top: 20px;
}
section.samples .ms-Button, section.setup .ms-Button {
display: block;
margin-bottom: 5px;
margin-left: 20px;
min-width: 80px;
}
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css
core-js@2.4.1/client/core.min.js
@types/core-js
jquery@3.1.1
@types/jquery@3.3.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment