我在这里做错了什么?我可以在没有 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
});
}