0

Hi I am trying to attach documents to a SharePoint list item using REST Api, but am getting an 404 error while performing. PFB my code:

function AddAttachments()
{   
    var digest = "";
    $.ajax(
    {
                    url: "/_api/contextinfo",
                    method: "POST",
                    headers: {
                                    "ACCEPT": "application/json;odata=verbose",
                                    "content-type": "application/json;odata=verbose"
                    },
                    success: function (data) {
                    digest = data.d.GetContextWebInformation.FormDigestValue;
                    },
                    error: function (data) {

                    }
    }).done(function() {
                    var fileInput = $('#uploadFile');
                    var fileName = fileInput[0].files[0].name;
                    var reader = new FileReader();
                    reader.onload = function (e) {
                    var fileData = e.target.result;
                        var res11 = $.ajax(
                        {                             
                                        url: _spPageContextInfo.webAbsoluteUrl+"/_api/web/lists/getbytitle('Test')/items(1)/AttachmentFiles/add(FileName='" + fileName + "')",                                       
                                        method: "POST",
                                        binaryStringRequestBody: true,
                                        data: fileData,
                                        processData: false,
                                        headers: {
                                                        "ACCEPT": "application/json;odata=verbose",          
                                                        "X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
                                                        "content-length": fileData.byteLength
                                        },                                                                                                                            
                                        success: function (data) {                                            
                                                console.log("success");                                               
                                        },
                                        error: function (data) {                                                
                                                console.log("Error occured." + data.responseText);
                                        }
                        });                          
                    };
                    reader.readAsArrayBuffer(fileInput[0].files[0]);

    });                                          
}

Thanks in advance.

4
  • Is the issue with multiple files uploads or even for single file its not working? Commented Oct 20, 2017 at 14:17
  • Multiple Files...Am able to upload single file but not multiple
    – Dheeraj
    Commented Oct 20, 2017 at 14:22
  • Just need to know how to iterate through multiple files
    – Dheeraj
    Commented Oct 20, 2017 at 14:24
  • check this answer sharepoint.stackexchange.com/a/154641/8068 and modify according to your requirement Commented Oct 20, 2017 at 19:23

1 Answer 1

0

Iterating through files as below

var files = $('#uploadFile')[0].files; 
for (var i = 0, f; f = files[i]; i++) {
    var reader = new FileReader();
    reader.onload = function (e) {
        //Logic to attach in SharePoint goes here
    };
    reader.readAsArrayBuffer(f);
}
5
  • How to get each file name from it?
    – Dheeraj
    Commented Oct 20, 2017 at 14:38
  • var fileName = f.name; Commented Oct 20, 2017 at 14:39
  • Able to add only one attachment..For second attachment it is throwing 409 conflict error
    – Dheeraj
    Commented Oct 20, 2017 at 14:46
  • Conflict is happening because your ajax call is async - make it synchronous by adding async:false Commented Oct 20, 2017 at 14:51
  • 1
    Not working...400 Bad Request error
    – Dheeraj
    Commented Oct 20, 2017 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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