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.

  • Is the issue with multiple files uploads or even for single file its not working? – Amal Hashim Oct 20 '17 at 14:17
  • Multiple Files...Am able to upload single file but not multiple – Dheeraj Oct 20 '17 at 14:22
  • Just need to know how to iterate through multiple files – Dheeraj Oct 20 '17 at 14:24
  • check this answer sharepoint.stackexchange.com/a/154641/8068 and modify according to your requirement – Gautam Sheth Oct 20 '17 at 19:23
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);
}
  • How to get each file name from it? – Dheeraj Oct 20 '17 at 14:38
  • var fileName = f.name; – Amal Hashim Oct 20 '17 at 14:39
  • Able to add only one attachment..For second attachment it is throwing 409 conflict error – Dheeraj Oct 20 '17 at 14:46
  • Conflict is happening because your ajax call is async - make it synchronous by adding async:false – Amal Hashim Oct 20 '17 at 14:51
  • 1
    Not working...400 Bad Request error – Dheeraj Oct 20 '17 at 16:16

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.