0

Need to upload a file to one document library SP2013 , and see the response in POSTMAN, using REST API, which format , which parameter to pass

0

Here is a completed demo about uploading file using Rest API, read file stream and then upload

 var folderName = "Folder1";
    var targetUrl = _spPageContextInfo.webServerRelativeUrl + "/" + documentLibrary + "/" + folderName;
    var url = webUrl + "/_api/Web/GetFolderByServerRelativeUrl(@target)/Files/add(overwrite=true, url='" + fileName + "')?$expand=ListItemAllFields @target='" + targetUrl + "'";

    uploadFileToFolder(uploadFileObj, url, function (data) {
        var file = data.d;
        var updateObject = {
            __metadata: {
                type: file.ListItemAllFields.__metadata.type
            },
            Column1: 'Test Data',   //meta data column1
            Column2: 'Test Data'    //meta data column2
        };

        url = webUrl + "/_api/Web/lists/getbytitle('"+documentLibrary+"')/items(" + file.ListItemAllFields.Id + ")";

        updateFileMetadata(url, updateObject, file, function (data) {
            alert("File uploaded & meta data updation done successfully");
        }, function (data) {
            alert("File upload done but meta data updating FAILED");
        });
    }, function (data) {
        alert("File uploading and meta data updating FAILED");
    });
}

function getFileBuffer(uploadFile) {
    var deferred = jQuery.Deferred();
    var reader = new FileReader();
    reader.onloadend = function (e) {
        deferred.resolve(e.target.result);
    }
    reader.onerror = function (e) {
        deferred.reject(e.target.error);
    }
    reader.readAsArrayBuffer(uploadFile);
    return deferred.promise();
}

function uploadFileToFolder(fileObj, url, success, failure) {
    var apiUrl = url;
    var getFile = getFileBuffer(fileObj);
    getFile.done(function (arrayBuffer) {
        $.ajax({
            url: apiUrl,
            type: "POST",
            data: arrayBuffer,
            processData: false,
            async: false,
            headers: {
                "accept": "application/json;odata=verbose",
                "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
            },
            success: function (data) {
                success(data);
            },
            error: function (data) {
                failure(data);
            }
        });
    });
}

function updateFileMetadata(apiUrl, updateObject, file, success, failure) {
    $.ajax({
        url: apiUrl,
        type: "POST",
        async: false,
        data: JSON.stringify(updateObject),
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "Content-Type": "application/json;odata=verbose",
            "X-Http-Method": "MERGE",
            "IF-MATCH": file.ListItemAllFields.__metadata.etag,
        },
        success: function (data) {
            success(data);
        },
        error: function (data) {
            failure(data);
        }
    });
}

Save the code in a js file, upload to Site Assets, then reference it in the page:

<script type="text/javascript" src="https://tarundev.sharepoint.com/sites/sharepointmates/SharepointMates/Scripts/jquery-3.1.1.js"></script>
<script type="text/javascript" src="https://tarundev.sharepoint.com/sites/sharepointmates/SharepointMates/Scripts/FileUploadScript.js"></script>

<input type="File" id="inputTypeFiles" />

<br>
<input type="button" id="btnUploadFiles" value="submit" Text="Upload"/>

Taken from here: Upload a file into document library using REST API in SharePoint 2013 / Online

  • 1
    thanks , But i'm look for API response through POSTMAN – Sham Jan 31 '18 at 10:29
  • @Jerry_MSFT what if access token is needed for share point connection . here we don't have any option to provide access token . – Anurag_BEHS Nov 2 '18 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.