0

I am trying to upload an attachment to my Sharepoint list and the upload goes well with a 200 message, however when I go to my list and try to open it, if it is a pdf will open an empty file with the same number of pages of the original file but no content and if it's a docx it won't open, it gives an error message. My code to upload is:

function upload() {
        var file;
        var count = 0;
        // $('input[type=file]').val(function (e) {
        file = $("#attachment-file")[0].files[0];
        // file = e.target.files[0];
        console.log(file);
        var reader = new FileReader();
        var getFileBuffer = function (file) {

            var deferred = $.Deferred();


            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) {
            timestamp = new Date().valueOf();
            var binary = "";
            var bytes = new Uint8Array(buffer);
            var i = bytes.byteLength;
            while (i--) {
                binary = String.fromCharCode(bytes[i]) + binary;
            }
            if (count == 0) {
                $http({
                    method: "POST",
                    url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/GetByTitle('RoutingForm')/Items("+Identification+")/AttachmentFiles/add(FileName='" + file.name + "')",
                    data: binary,
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                        "content-length": binary.byteLength
                    }
                })

                .then(function (success) {
                    count++;
                    // $scope.attachments.push({ name: file.name, link: mainPage + "/Lists/OnBoarding/Attachments/" + Id + "/" + file.name })
                    alert("You have attached the file succesfully!");
                    return;
                },
                function (error) {
                    count++;
                    console.log(error);
                    alert("An error has occured with the attachment. No file has been attached.");
                });
            }
        });
    }
0

I think that you just need to modify the file buffer in

getFileBuffer(file).then(function (buffer) {
}

block you use below code and try

getFileBuffer(file).then(function (buffer) {
        timestamp = new Date().valueOf();
        var binary = "";
        var bytes = new Uint8Array(buffer);
        var content = new SP.Base64EncodedByteArray(); 

And in

$http({
                method: "POST",
                url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/GetByTitle('RoutingForm')/Items("+Identification+")/AttachmentFiles/add(FileName='" + file.name + "')",
                data: buffer,
                processData: false,
                headers: {
                    "accept": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                    "content-length": buffer.byteLength
                }
            })
  • Hi @Sunil! thanks for your input but it still doesn't work.. I also didn't understand what the content variable is doing that if it is not doing anything. Did you mean to use it somewhere else? Thanks again! – Luana Mayara Fernandes May 30 at 19:05
  • Yes you can skip it, try to processData: false, in ajax call. – Sunil Sahu May 31 at 9:32
  • I tried :( it did not work. – Luana Mayara Fernandes Jun 4 at 15:53

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.