通过上传API Yammer的文件 [英] Uploading a file via Yammer API
问题描述
我能够发布一条消息,但是当我添加的任何附件或pending_attachment,我得到一个错误说:
类型错误:升压所谓的对象不实现接口HTMLInputElement上
功能后(){
yam.getLoginStatus(功能(响应){
如果(response.authResponse){ yam.request(
网址:{url:https://api.yammer.com/api/v1/messages.json//注:端点api.yammer ...
,方法:POST
数据:{
身体:的document.getElementById(post_body)值,
GROUP_ID:的document.getElementById(GROUP_ID)值
附件1:的document.getElementById(附件)
}
,成功:函数(MSG){
警报(后成功!:+ msg.messages [0] .ID); //新的消息的ID
}
,错误:函数(MSG){警报(后是不成功的。+味精); }
}
);
}其他{
yam.login(功能(响应){
//没有
});
}
});
}
哭诉的JavaScript SDK不带附件工作。 (至少没有工作的例子已经看到在互联网上)上传附件,您可以将文件上传到服务器,然后使用og_url来发表您的服务器上的一个链接到该文件,或做你自己的Ajax表单上传。这里有一个例子:
VAR数据=新FORMDATA(); data.append(身体,的document.getElementById(post_body)值。);
data.append('GROUP_ID'的document.getElementById(GROUP_ID)值。);
data.append(附件1,的document.getElementById(附件),filename_of_your_choice');
$阿贾克斯({
网址:https://api.yammer.com/api/v1/messages.json
数据:数据,
beforeSend:功能(XHR){
//设置授权头
xhr.setRequestHeader(授权,承载YOUR_AUTHORIZATION_TOKEN);
},
缓存:假的,
的contentType:假的,
过程数据:假的,
输入:POST,
成功:功能(数据){
的console.log(AJAX岗位上取得成功。);
},
错误:功能(XMLHtt prequest,textStatus,errorThrown){
警报(有与该请求的错误。);
}
});
注意,授权令牌的响应登录成功获得。这是不是你的应用程序ID。此外,我怀疑的document.getElementById(附件)会工作。你需要的对象转换为字节数组一滴。
I'm able to post a message but when I add either the attachment or pending_attachment, I get an error saying:
TypeError: 'stepUp' called on an object that does not implement interface HTMLInputElement.
function post() {
yam.getLoginStatus( function(response) {
if (response.authResponse) {
yam.request(
{ url: "https://api.yammer.com/api/v1/messages.json" //note: the endpoint is api.yammer...
, method: "POST"
, data: {
"body" : document.getElementById("post_body").value,
"group_id" : document.getElementById("group_id").value
,"attachment1" : document.getElementById("attachment")
}
, success: function (msg) {
alert("Post was Successful!: " + msg.messages[0].id); //id of new message
}
, error: function (msg) { alert("Post was Unsuccessful..." + msg); }
}
);
} else {
yam.login( function (response) {
//nothing
});
}
});
}
yammer's javascript SDK doesn't work with attachment. (at least no working example has been seen on the internet) To upload an attachment, you can either upload the file to your server and then use og_url to post a link to that file on your server, or cook up your own ajax form upload. here is an example:
var data = new FormData();
data.append('body', document.getElementById("post_body").value);
data.append('group_id', document.getElementById("group_id").value);
data.append('attachment1', document.getElementById("attachment"), 'filename_of_your_choice');
$.ajax({
url: "https://api.yammer.com/api/v1/messages.json",
data: data,
beforeSend: function (xhr) {
// set authorization header
xhr.setRequestHeader("Authorization", "Bearer YOUR_AUTHORIZATION_TOKEN");
},
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function (data) {
console.log("ajax post success.");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("There was an error with the request.");
}
});
Notice that the authorization token is obtained in the response to a successful login. It is not your app ID. Also, I doubt document.getElementById("attachment") will work. You need to convert that object into an byte array blob.
这篇关于通过上传API Yammer的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!