MSTr
MSTr

Reputation: 223

Yammer attachement

I'm trying to post a message with an attachment image via my application so I used this code :

function yamPost(mytoken) {
        var msg_Body = jQuery("#myBody").val();
        var imgData = new FormData();

        var files = $("#attachement").get(0).files;

        if (files.length > 0) {
            imgData.append("attachement1", files[0]);
        }

        yam.platform.request(
        {
            url: "messages.json",
            method: "POST",
            network: "myNetwork",
            header:{
                authorization: "Bearer " + mytoken,
            },
            data: {
                body: msg_Body,
                attachment1: imgData
            },
            contentType: "multipart/form-data",

            success: function (feedResult) {
                console.log("- Yeaaahhh");
                console.log(feedResult);
                debugger;
            },
            error: function (msg) {
                console.log("- Can't post message with attachement");
                debugger;
            }
        });
    }

and I got error:

"Uncaught TypeError: Illegal invocation"

I also tried to use pending_attachment but I have faced same problem or http500 internal server error... could anyone please post an example code of how he do it.

EDIT

I tried a new approach sending the data :

instead of sending a FormData i used this :

function readBinary() {
        var reader = new FileReader();

        reader.onloadend = function () {
            yamPost(reader.result, token);
        }

        reader.readAsBinaryString(document.getElementById("attachement").files[0]);
    }

and each time i do that i faced this exception :

"No 'Access-Control-Allow-Origin'"

However if i don't include the attachment (attachment1: imgData or binaryFileOutput) the message is posted !

Upvotes: 2

Views: 361

Answers (1)

MSTr
MSTr

Reputation: 223

I finally found an answer, In fact, using the FormData Object was the correct thing to do but I use it on the worng way, you have to append all the properties that you will send on the same FormData Object just like this thread, and here is the magic code:

function YamPostImage() {
var myData = new FormData();

myData.append('body', "my app images");
myData.append('attachment1', document.getElementById("attachement").files[0]);

yam.platform.request(
{
    url: "messages.json",
    method: "POST",
    network: "my-network",
    data: myData,
    cache: false,
    contentType: false,
    processData: false,
    success: function (feedResult) {
        console.log("- Yeaaahhh");
        debugger;
    },
    error: function (msg) {
        console.log("- Can't post message with attachement");
        debugger;
    }
});

}

Upvotes: 2

Related Questions