Skip to content

Instantly share code, notes, and snippets.

@beckettkev
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beckettkev/0a583a8dc398497d8fe0 to your computer and use it in GitHub Desktop.
Save beckettkev/0a583a8dc398497d8fe0 to your computer and use it in GitHub Desktop.
//first off, get the Base64 image value from the Web API method
$.ajax({
url: "https://" + window.location.host + "/api/values?path=" + url
}).done(function (data) {
//data represents the Base64 value of the image
if (data != null) {
if (data.length > 0) {
var imageOXmlTemplate;
var templateRequest = new XMLHttpRequest();
var base64;
//Get the Open XML image snippet
templateRequest.open('GET', '/Scripts/App/Templates/Image.xml', false);
templateRequest.send();
if (templateRequest.status === 200) {
imageOXmlTemplate = myOOXMLRequest.responseText;
//Save the Base64 value in a local variable (after removing the content type prefix)
base64 = (data.substr(data.indexOf(',') + 1));
}
/*
...
Here you would update the snippet, with a find and replace using the file type (PNG, JPEG etc..).
Maybe something like...
imageOXmlTemplate = imageOXmlTemplate.replace(/{format}/g, url.substr(url.lastIndexOf('.') + 1));
*/
Office.context.document.setSelectedDataAsync(
imageOXmlTemplate.replace(
'{Base64}',
base64
),
{ coercionType: "ooxml" },
function (asyncResult) {
if (asyncResult.status == "failed") {
//maybe do something?
//asyncResult.error.message;
} else {
//you should be able to see the image in your document
}
}
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment