2014-07-09 6 views
0

나는 메시지를 게시 할 수 있어요하지만 첨부 파일 또는 pending_attachment 중 하나를 추가 할 때, 내가 말하는 오류가 발생합니다 :불평 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 's javascript SDK는 첨부 파일과 함께 작동하지 않습니다. (적어도 인터넷에서는 작동 예제가 없습니다.) 첨부 파일을 업로드하려면 서버에 파일을 업로드 한 다음 og_url을 사용하여 서버에 해당 파일에 대한 링크를 게시하거나 자신의 아약스 양식 업로드를 작성하십시오 . 다음은 예입니다.

 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 ("attachment")가 작동하지 않을지 의심 스럽습니다. 해당 객체를 바이트 배열 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>

관련 문제