如何将本地图像编码为multipart / form-data以发布到Facebook

时间:2014-12-26 06:19:21

标签: javascript facebook facebook-graph-api facebook-javascript-sdk

我正试图将媒体发布到Facebook的页面,使用来自JavaScript sdk的FB.api(“/ {page_id} / photos”)管理我。但我无法成功。

如何编码将本地图片文件作为可以发布到Facebook的正确 multipart / form-data

function onChangeMediaReader() {
  var file = document.getElementById('post-media').files[0];
  reader = new FileReader();
  reader.onload = function() {
    // displays the selected image in canvas.
    document.getElementById('post-media-src').src = reader.result;
    document.getElementById('media-container').style.display = "block";

    // form-data of the selected image for source param.
    formData = new FormData();
    formData.append("source", reader.result);
  }

  if (file) {
    reader.readAsDataURL(file);
  }
}

function publishPost() {
  params.access_token = page_access_token;
  params.message = document.getElementById('post-message').innerHTML;

  if (formData) {
    params.source = formData;
  }

  FB.getLoginStatus(function(response) {
    FB.api(
      '/' + page_id + '/photos',
      "POST",
      params,
      function(response) {
        console.log(response);
      });
  });
}
<form id="image-data" method="post" enctype="multipart/form-data">
  <input type="file" name="source" id="post-media" accept="image/*" onchange="onChangeMediaReader();" />
  <label for="post-media">Upload media</label>
  <br />
  <br />
</form>

<button id="publish-post" onclick="publishPost();">Submit</button>

回复消息

“(#324)需要上传文件”,
“type:”OAuthException“

0 个答案:

没有答案