1

我在这里做错了什么?我可以在没有 SP.AppContextSite 代码和更改两个 URL 的情况下上传到同一个网站集。所有 HTML 标记和 jquery 都嵌入到 /sites/dev 上的页面中。目标库是 /test/Documents。我对这两个网站集都拥有完全权限。

  function ProcessUpload(fileInput) {
    var reader = new FileReader();
    reader.onload = function (result) {
        var fileName = '',
         libraryName = '',
         fileData = '';

        var byteArray = new Uint8Array(result.target.result)
        for (var i = 0; i < byteArray.byteLength; i++) {
            fileData += String.fromCharCode(byteArray[i])
        }

        // once we have the file perform the actual upload
        PerformUpload(fileInput.name, fileData);

    };
    reader.readAsArrayBuffer(fileInput);
}

function PerformUpload(fileName, fileData) {
    var url;



        url = 'https://mytenant.sharepoint.com/sites/dev' +
        "/_api/SP.AppContextSite(@TargetSite)/web/lists/getByTitle(@TargetLibrary)/RootFolder/Files/add(url=@TargetFileName,overwrite='false')?"+
          "@TargetSite='" + 'https://mytenant.sharepoint.com/sites/test'+ "'" +
            "&@TargetLibrary='Documents'" +
            "&@TargetFileName='CV" + fileName + "'";
            alert(url);


    // use the request executor (cross domain library) to perform the upload

$.ajax({     
        url: url,
        type: "POST",
        headers: {
            "Accept": "application/json; odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()

        },
        data: JSON.stringify({ '__metadata': { 'type': 'SP.List' }, 'Title': 'New title' }),


        contentType: "application/json;odata=verbose",
        binaryStringRequestBody: true,
        body: fileData,
        success:successHandler,
        error: errorHandler
    });
}
4

2 回答 2

0

尝试在所有 uri 参数值上添加 encodeURIComponent()。这确保它们在服务器上被正确读取。

url = 'https://mytenant.sharepoint.com/sites/dev' + "/_api/SP.AppContextSite(@TargetSite)/web/lists/getByTitle(@TargetLibrary)/RootFolder/Files/add(url=@TargetFileName,overwrite='false')?"+
      "@TargetSite='" + encodeURIComponent('https://mytenant.sharepoint.com/sites/test')+ "'" +
      "&@TargetLibrary='Documents'" +
      "&@TargetFileName='CV" + encodeURIComponent(fileName) + "'";
于 2013-10-03T08:17:20.210 回答
0

尝试从 url = ' https://mytenant.sharepoint.com/sites/dev ' + TO _spPageContextInfo.webServerRelativeUrl +更改您的 url 字符串,另外,使用 executeAsync。

$.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js", PerformUpload(fileName, fileData));

函数 PerformUpload(fileName, fileData) { var url;

    url = _spPageContextInfo.webServerRelativeUrl +        "/_api/SP.AppContextSite(@TargetSite)/web/lists/getByTitle(@TargetLibrary)/RootFolder/Files/add(url=@TargetFileName,overwrite='false')?"+
      "@TargetSite='" + 'https://mytenant.sharepoint.com/sites/test'+ "'" +
        "&@TargetLibrary='Documents'" +
        "&@TargetFileName='CV" + fileName + "'";
        alert(url);

    var executor = new SP.RequestExecutor(_spPageContextInfo.webServerRelativeUrl);
       executor.executeAsync(
            {
          url: url,

方法:“POST”,标题:{“接受”:“应用程序/json;odata=verbose”,“X-RequestDigest”:$(“#__REQUESTDIGEST”).val()

    },
    data: JSON.stringify({ '__metadata': { 'type': 'SP.List' }, 'Title': 'New title' }),


    contentType: "application/json;odata=verbose",
    binaryStringRequestBody: true,
    body: fileData,
    success:successHandler,
    error: errorHandler

}

于 2015-05-05T00:45:16.177 回答