0

I try to get an Image from SharePoint by using JavaScript code. I have the complete URL of the image which is stored in a SharePoint Assets Library. My app is SharePoint hosted. This code does not work:

var fileUrl;

var clientContext = SP.ClientContext.get_current();
var oWebsite = clientContext.get_web();

clientContext.load(oWebsite);
var base = this;

clientContext.executeQueryAsync(function() {
    var fileUrl = "/sites/collection/assets/picture.jpg";

    var digest = $("#__REQUESTDIGEST").val();

    $.ajax({
        url: fileUrl,
        contentType: "image/jpeg",
        type: "GET",
        timeout: 500,
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": digest
        },
        success: (response) => {
            base.successHandler(response);
        },
        error: (xhr) => {
            base.errorHandler();
        }
    });

I get no error.

2

I believe you need to use XMLHttpRequest for this

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
    if (this.readyState == 4 && this.status == 200){
        //this.response is what you're looking for
        handler(this.response);
        console.log(this.response, typeof this.response);
        var img = document.getElementById('img');
        var url = window.URL || window.webkitURL;
        img.src = url.createObjectURL(this.response);
    }
}
xhr.open('GET', fileUrl);
xhr.responseType = 'blob';
xhr.send();  

https://stackoverflow.com/questions/17657184/using-jquerys-ajax-method-to-retrieve-images-as-a-blob

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.