2014-07-09 118 views
0

我能夠發佈消息,但是當我添加任何附件或pending_attachment,我得到一個錯誤說:上傳通過Yammer的API文件

類型錯誤:「升壓」調用的對象不落實接口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 
      }); 
     } 
    }); 
} 

回答

1

yammer的javascript SDK不能與附件一起使用。 (至少在互聯網上沒有看到任何工作示例)要上傳附件,您可以將文件上傳到服務器,然後使用og_url在服務器上發佈指向該文件的鏈接,或製作自己的ajax表單上傳。這裏是一個例子:

 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."); 
      } 
     }); 

請注意,授權令牌是在成功登錄的響應中獲得的。這不是你的應用ID。此外,我懷疑document.getElementById(「附件」)將工作。您需要將該對象轉換爲字節數組blob。

1

它爲我的作品:

function postAttach() { 
 
\t var msg = $('#attach_body').val(); 
 
\t 
 
\t var m_data = new FormData(); 
 
\t m_data.append('body', msg); 
 
\t m_data.append('group_id', 6194208); 
 
\t m_data.append('attachment1', $('input[name=attachment1]')[0].files[0]); 
 

 
\t yam.platform.request({ 
 
\t \t 
 
\t \t url: "messages.json",  
 
\t \t contentType: "multipart/form-data", 
 
\t \t data: m_data, 
 
\t \t processData: false, 
 
\t \t contentType: false, 
 
\t \t type: 'POST', 
 
\t \t dataType: 'json', 
 
\t \t success: function (user) { 
 
\t \t \t alert("The request was successful."); 
 
\t \t }, 
 
\t \t error: function (user) {console.log(user); 
 
\t \t \t alert("There was an error with the request."); 
 
\t \t } 
 
\t }); 
 
}
<div name="postYammer"> 
 
    \t <input type="text" name="body" value="" id="attach_body" /> 
 
    \t <input type="file" name="attachment1" id="attach_img"/> 
 
    \t <button onclick="postAttach()" type="button">Post</button> 
 
    </div>