2

I have been scratching my head the all thought day figuring out a way to download non-text files like images from host SharePoint web into a App web (SharePoint hosted app). The code below allows me to get the contents of txt files but when requested for images, I am not able to get the image datauri or contents.

Is this possible via JavaScript/REST to get image contents?

var hostweburl,appweburl= null;
jQuery(document).ready(function () {
//Get the URI decoded URLs.
hostweburl = decodeURIComponent(
    getQueryStringParameter("SPHostUrl")
    );
appweburl = decodeURIComponent(
    getQueryStringParameter("SPAppWebUrl")
    );

var scriptbase = hostweburl + '/_layouts/15/';
jQuery.getScript(scriptbase + 'SP.Runtime.js',
   function () {
       jQuery.getScript(scriptbase + 'SP.js',
           function () { jQuery.getScript(scriptbase + 'SP.RequestExecutor.js', continueExecution); }
       );
   });

function getQueryStringParameter(paramToRetrieve) {
    var params =
        document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve)
            return singleParam[1];
    }
}

function continueExecution() {
    var executor;
    executor = new SP.RequestExecutor(appweburl);
    var urlp = appweburl + "/_api/SP.AppContextSite(@target)/web/GetFolderByServerRelativeUrl('/sites/MyAppHost/PublishingImages')/Files('fdef1543-0466-40e5-94b7-35af75ad262d.jpg')/$value?@target='" + hostweburl + "'";
    executor.executeAsync(
     {
         url: urlp,
         method: "GET",
         headers: { "Accept": "application/json; odata=verbose" },
         contentType: "image/png",
         success: successHandler,
         error: errorHandler,
     });
}

function successHandler(data) {

}

function errorHandler(error) {
}

});

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.