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.

3
  • Are you trying to read image bytes? Commented Apr 29, 2015 at 11:42
  • Yes, I need the bytes Commented Apr 29, 2015 at 11:51
  • stackoverflow.com/a/10646659/1375553 Commented Apr 29, 2015 at 12:34

1 Answer 1

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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.