Create a gist now

Instantly share code, notes, and snippets.

Embed
//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