SharePoint Online:上传附件-JavaScript

时间:2019-05-30 02:23:44

标签: javascript sharepoint sharepoint-online

我正在尝试获取附件输入以上传到我的SharePoint列表。 除了附件,它会提交。

按钮有一个onclick="createItem()":使用脚本sp.runtime,sp,SPServices

预先感谢

HTML

<div class="form-group row">
    <div class="col-md-12">
        <label for="attachment">Attachment:</label>
        <input id="myAttachments" type="file" fileread="run.AttachmentData" 
               fileinfo="run.AttachmentInfo" class="form-control" />
    </div>
</div>
function createItem( listItemType ) {
    var itemUrl = baseURI + siteName + "/_api/web/lists/getByTitle('" + listName + "')/items";
    var itemType = getListItemType( listName );
    var item = {
        "__metadata": {
            "type": itemType
        },
        "Title": $( '#name' ).val()
    };

    var body = JSON.stringify( item );
    $.ajax( {
        url: itemUrl,
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: body,
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $( "#__REQUESTDIGEST" ).val()
        },
        success: function ( data ) {
            // alert( "Item Created" );
            submitAttachment( data.d.ID );
            $( '#successAlert' ).show(); //** Shows Modal after submit */
            setTimeout( function () {
                location.reload()
            }, 2000 );
        },
        error: function ( data ) {
            console.log( data );
        }
    } );
}

// Getting the item type for the list
function getListItemType( name ) {
    return "SP.Data." + name[ 0 ].toUpperCase() + name.substring( 1 ) + "ListItem";
}

function submitAttachment( ID ) {
    var file = $( '#myAttachments' )[ 0 ].files[ 0 ];
    var getFileBuffer = function ( file ) {
        var deferred = $.Deferred();
        var reader = new FileReader();

        reader.onload = function ( e ) {
            deferred.resolve( e.target.result );
        }

        reader.onerror = function ( e ) {
            deferred.reject( e.target.error );
        }

        reader.readAsArrayBuffer( file );

        return deferred.promise();
    };

    getFileBuffer( file ).then( function ( buffer ) {

        $.ajax( {
            url: _spPageContextInfo.webAbsoluteUrl +
                "/_api/web/lists/getbytitle('" + list + "')/items(" + ID + ")/AttachmentFiles/add(FileName='" + file.name + "')",
            method: 'POST',
            data: buffer,
            processData: false,
            headers: {
                "Accept": "application/json; odata=verbose",
                "content-type": "application/json; odata=verbose",
                "X-RequestDigest": document.getElementById( "__REQUESTDIGEST" ).value
                //"content-length": buffer.byteLength
            }
        } );

    } );
}
  

未捕获的TypeError:无法读取未定义的属性'overrideRequest'

0 个答案:

没有答案