Javascript Sharepoint错误';要将项目添加到文档库,请使用SPFileCollection.add()';通过REST基于内容类型添加空白项时

Javascript Sharepoint错误';要将项目添加到文档库,请使用SPFileCollection.add()';通过REST基于内容类型添加空白项时,javascript,rest,sharepoint,sharepoint-online,Javascript,Rest,Sharepoint,Sharepoint Online,我正在尝试向文档库中添加一个项目,该项目是文档库中内容类型的空白版本。从本质上讲,我复制内容类型,并使用名称将其扔进文档库,但不更改内容。我收到这个错误 乐码 // Submit add action form submitAddActionForm = function() { // Get submitted values var aptype = document.getElementById('form-action-aptype').value; var sy

我正在尝试向文档库中添加一个项目,该项目是文档库中内容类型的空白版本。从本质上讲,我复制内容类型,并使用名称将其扔进文档库,但不更改内容。我收到这个错误

乐码

// Submit add action form
submitAddActionForm = function() {
    // Get submitted values
    var aptype = document.getElementById('form-action-aptype').value;
    var system = document.getElementById('form-action-system').value;
    var name = document.getElementById('form-action-name').value + ".xlsx";
    var start = document.getElementById('form-action-start').value;
    var duration = parseInt(document.getElementById('form-action-duration').value);
    var end = new Date(start).addHours(duration);
    var className = 'red';
    var shape = 'range';

    // Add item to sharepoint
    createAPinSPList(aptype,name,system,start,duration);

    // Add item to timeline
    addToTimeline(system,name,start,end,className,type,shape);

    // Remove form and grayout
    $('#form-action, #gray-out').remove();

    // Log change
    console.log("New Item:");
    console.log(aptype,system,name,start,end,className,shape);
}

// Creates AP in SP List
function createAPinSPList(aptype,actionName,system,start,duration) {

    // vars
    var contentTypeId;

    // designate content type
    switch (aptype) {
        case "SCN":
            contentTypeId = '0x0101003C20115C4BCA9548B977850AFE3B786E030043111C2A772F8C48B261CEF7F48FF87D';
            break;

        case "SRC":
            contentTypeId = '0x0101003C20115C4BCA9548B977850AFE3B786E010026F40826B07DEB48A77D5EC704A27343';
            break;

        case "DL":
            contentTypeId = '0x0101003C20115C4BCA9548B977850AFE3B786E0200E9BCBF0AE424FC4AA04F78ECC2569BDF';
            break;

        default:
            break;
    }

    // create metadata
    var data = { 
        __metadata: {'type': 'SP.Data.NXE_x0020_AP_x0020_TrackerItem'},
        Title: actionName,
        FileLeafRef: actionName,
        Action: actionName,
        System: system,
        Planned_x0020_Start: start,
        Required_x0020_Time_x0020__x0028_hrs_x0029_: duration,
        ContentTypeId: contentTypeId        
    };

    // create item in SP list
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('NXE Action Plan Tracker')/items",
        type: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "content-Type": "application/json;odata=verbose"
        },
        data: JSON.stringify(data),
        success: function (data) {
            console.log("Created successfully!")
        },
        error: function (error) {
            alert("cannot add for some reason");
            alert(JSON.stringify(error));
        }
    }); 
}
我有一个表单,当用户提交一些信息时,它会调用“submitAddActionForm”

有些字段是必需的,我没有将其与元数据并行传递,但我认为这不是问题所在。另外,vis.js timeline还有一些额外的代码,希望不会让人混淆


非常感谢您的帮助。我找不到任何具有这种特殊性的资源。

要将文件添加到文档库,请使用以下端点:

/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files/Add(url='file name', overwrite=true)
    method: POST
    body: contents of binary file
    headers:
        Authorization: "Bearer " + accessToken
        X-RequestDigest: form digest value
        content-type: "application/json;odata=verbose"
        content-length:length of post body


您可以检查共享的示例代码。

如何获取二进制文件?我应该在之前做一个请求吗?我在使用FileReader获取文档库中的项目时遇到了很多麻烦。您可以修改代码以显示示例吗?
/_api/web/lists/getbytitle('Documents')/rootfolder/files/add(url='file name',overwrite=true)