1

When using this code for adding files:

 $("#getFile").on('change', function () {
    var idVal = $("#" + itemID).val();
    var files = this.files;
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists('" + guidList + "')/items",            
        method: "POST",
        ....

    });

    for (var i = 0; i < files.length; i++) {
            var file = files[x];
            var lastFileName = file.name;
            if (!checkIfExist(filesItems, lastFileName)) {
                var fileName = Date.now() + file.name;
                var getFileBuffer = function (file) {
                    //alert('in getFileBuffer');
                    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('" + guidList + "')/items('" + idVal + "')/AttachmentFiles/add(FileName = '" + fileName + "')",
                        method: 'POST',
                        ....

    })

I get an error for sending a post to this address: .../_api/web/lists('9d082dbb-217c-4abb-922f-807d0f6d5dce')/items('22')/AttachmentFiles/add(FileName = 'fileName')

Do you know how to use rest API for adding attachments to a specific file by the list guid?

(When using by list name - it works)

  • add(FileName = '" + fileName + "')" why do you have spaces before and after FileName. It should be add(FileName='" + fileName + "')" – Arsalan Adam Khatri Mar 1 '18 at 13:57
0

This has worked for me in the past.

"/_api/web/lists(guid'" + guidList + "')/items"
  • Could you reflect that in the vote? – AtariPixel Mar 5 '18 at 5:09
  • I have problem in voting because my reputation is 6 – Eyal Segal Mar 5 '18 at 6:51
  • Can't up vote in your own post? – AtariPixel Mar 5 '18 at 7:22

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.