1

I created Share Point hosted app. When I select Document from a document Library file should be select and create a zip of selected file.

I am getting a file Content using following rest Call

var hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));

var executor = new SP.RequestExecutor(appweburl);
executor.executeAsync(
{
    url: appweburl + "/_api/SP.AppContextSite(@target)/web/GetFileByServerRelativeUrl('" + filePath + "')/$value?@target='" + hostweburl + "'",
    type: "GET",
    success: readContents,
    error: function (xhr) {
        alert(xhr.status + ": " + xhr.statusText)
    }
});
function readContents(data) {

var blob = new Blob([data.body], { type: "application/octet-binary" });


 //---I'm getting blob content

//Now I want to pass this blob to jszip to create a zip . I have tried this. I converted BLOB to base 64 



blobToBase64(blob, function (binaryData) {
    alert(binaryData);

    var zip = new JSZip();
    zip.file("a.txt", binaryData, { base64: true });

    zip.generateAsync({ type: "base64" }).then(function (binaryData) {
        location.href = "data:application/zip;base64," + binaryData;
    });
});

}


function base64ToArrayBuffer(base64) {
    var binary_string = window.atob(base64);
    var len = binary_string.length;
    var bytes = new Uint8Array(len);
    for (var i = 0; i < len; i++) {
        bytes[i] = binary_string.charCodeAt(i);
    }
    return bytes.buffer;
}

This code works fine when there is .txt file but not work for .pdf,.docx file. A file is downloaded successfully but a file doesn't content original data that is already on server.

Please suggest me to do this thing!

  • Here data.body is a blob format or binary format? – Dhruvin Shah Oct 27 '16 at 11:24

Your Answer

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

Browse other questions tagged or ask your own question.