Last active
August 29, 2015 14:21
-
-
Save beckettkev/0a583a8dc398497d8fe0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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