使用JavaScript获取SP.File对象的附件二进制文件

时间:2018-04-03 10:04:24

标签: javascript sharepoint-2013 sp.js

我在检索附件列表项的二进制文件时遇到问题。我使用此代码来获取值:

var appweburl = window.location.protocol + "//" + window.location.host
    + _spPageContextInfo.webServerRelativeUrl;

var hostweburl = _spPageContextInfo.siteAbsoluteUrl;

var executor = new SP.RequestExecutor(appweburl);

executor.executeAsync({
    url: appweburl + "/_api/SP.AppContextSite(@target)/web/getfilebyserverrelativeurl('/Lists/myList/Attachments/1/myImage.jpeg')/$value?@target='" + hostweburl + "'",
    method: "GET",
    binaryStringResponseBody: true,
    success: function (data) {
        console.log(data.body);
    },
    error: function (err) {
        alert(JSON.stringify(err));
    },
});

但它让我回到了这个错误:

    GET "https://mySharePointAddress/Site/Subsite/web/_api/SP.AppContextSite(@target)/web/getfilebyserverrelativeurl('/Lists/myList/Attachments/1/myImage.jpeg')/$value?@target=%27https://mySharepointAddress/Site%27 403 (Forbidden)

    Uncaught ReferenceError: ret is not defined
    at Function.SP.RequestExecutorInternalSharedUtility.$13 (SP.RequestExecutor.js:2)
    at Function.SP.RequestExecutor.internalProcessXMLHttpRequestOnreadystatechange (SP.RequestExecutor.js:2)
    at XMLHttpRequest.<anonymous> (SP.RequestExecutor.js:2)

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

我使用jquery ajax找到了我的问题的解决方案,谢谢Henry Algus http://www.henryalgus.com/reading-binary-files-using-jquery-ajax/