List AddAttachment

Sep 26, 2014 at 8:21 PM
Edited Sep 26, 2014 at 8:21 PM
I'm trying to figure out what i'm doing wrong here.

I'm using javascript file reader to get the contents of a file as binary.

When I try to add the attachment, the XML/SOAP is containing literally [object ArrayBuffer] instead of the actual Array.
var ID = 11; //some other truncated code but it is getting the correct ID into this variable

var reader = new FileReader();
reader.readAsArrayBuffer(doc);
reader.onload = function (event) {

    var fileName = document.getElementById('upload').files[0].name; 
            
    $().SPServices({
        operation: "AddAttachment",
        async: false,
        listName: "ListTest",
        listItemID: ID,
        fileName: fileName,
        attachment: event.target.result,
        completefunc: function (xData, Status) {
            console.log(xData.responseXML);
        }
    });
The SOAP messagegetting sent to sharepoint server
<soap:Envelope 
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
    xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
    xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
    
    <soap:Body>
        <AddAttachment xmlns='http://schemas.microsoft.com/sharepoint/soap/'>
            <listName>ListTest</listName>
            <listItemID>11</listItemID>
            <fileName>test.htm</fileName>
            <attachment>[object ArrayBuffer]</attachment>
        </AddAttachment>
    </soap:Body>
</soap:Envelope>
Any advice on what i'm doing wrong here? Why is it converting the actual array into the string [object ArrayBuffer]
Sep 26, 2014 at 9:13 PM
Edited Sep 26, 2014 at 9:18 PM
Ok i figured it out... it should be obvious I needed to base 64 encode the array.

I was trying to pass it directly because MSDN link shows

http://msdn.microsoft.com/en-us/library/lists.lists.addattachment(v=office.12).aspx
[SoapDocumentMethodAttribute("http://schemas.microsoft.com/sharepoint/soap/AddAttachment", RequestNamespace="http://schemas.microsoft.com/sharepoint/soap/", ResponseNamespace="http://schemas.microsoft.com/sharepoint/soap/", Use=SoapBindingUse.Literal, ParameterStyle=SoapParameterStyle.Wrapped)] 
public string AddAttachment (
    string listName,
    string listItemID,
    string fileName,
    byte[] attachment
)
Shows the attachment parameter is a byte[] but of course that doesn't make sense in a SOAP message, i was wrongly assuming from this comment:
attachment
A byte array that contains the file to attach by using base-64 encoding.
that the SPServices was doing the base-64 encoding
Coordinator
Sep 28, 2014 at 9:56 AM