0

I used readasarraybuffer to read the input image and changed the array to base64 format. Then I used rest api post to upload the image to sharepoint image library, and image got uploaded but it is empty (0 kb) showing white document icon.

  • 1
    You need to show all of your code. You now ask "Yo, guys, my car doesn't go forward, what is wrong?" – Danny '365CSI' Engelman Feb 2 '17 at 11:28
0

To upload an image or a file to SharePoint with Full rest you have to pass an array buffer not a base64 string.

var getFile = getFileBuffer(v);
getFile.done(function (arrayBuffer) {
    getFile.done(function (arrayBuffer) {
    var doUpload = uploadDocument(arrayBuffer, fileName);
    doUpload.done(function (file, status, xhr) {
        //the rest of the magic
    });
});

function uploadDocument(buffer, fileName) {
    var url = _spPageContextInfo.webAbsoluteUrl + '/_api/web/Lists/GetbyTitle(\'your_library\')/RootFolder/Files/add(url=@TargetFileName,overwrite=\'true\')?' +
        '&@TargetFileName=\'' + encodeURI(fileName) + '\'';
    var call = $.ajax({
        url: url,
        type: 'POST',
        data: buffer, //<-- This is the file
        processData: false,
        headers: {
            Accept: 'application/json;odata=verbose',
            'X-RequestDigest': $('#__REQUESTDIGEST').val()
        }
    });
    return call;
}

function getFileBuffer(v) {
    var deferred = jQuery.Deferred();
    var reader = new FileReader();
    reader.onloadend = function (e) {
        deferred.resolve(e.target.result);
    }
    reader.onerror = function (e) {
        deferred.reject(e.target.error);
    }
    reader.readAsArrayBuffer(v);
    return deferred.promise();
}

The code above is what I use to upload heavy documents to SharePoint. I hope it helps you :)

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.