Facebook Javascript表单数据照片上传:需要上传文件错误 [英] Facebook Javascript Form Data Photo Upload: requires upload file error

查看:142
本文介绍了Facebook Javascript表单数据照片上传:需要上传文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎每个帖子都搜索了15个小时,但没有找到问题的答案。我正在使用严格的Javascript使用表单数据将照片上传到Facebook。我使用这种解码方法( canvas元素)解码了base64数据。 binary.jsrel =nofollow> https://github.com/danguer/blog-examples/blob/master/js/base64-binary.js )然后将其转换为表单中的字符串数据格式,如 https://gist.github.com/andyburke/1498758 (不使用XMLHttpRequest) 。然后我将该字符串作为帖子中的source参数。我一直收到(#324)需要上传文件。如果我只是为图像添加一个网址,它的效果非常好。有人可以帮忙吗?谢谢!

I've searched nearly every thread for 15-some hours and haven't found an answer to my problem. I'm using strictly Javascript to upload a photo to Facebook using form data. I decoded the base64 data from my JPEG image (originally svg -> canvas element), using this decode method (https://github.com/danguer/blog-examples/blob/master/js/base64-binary.js) and then converting it to a string in the form data format like https://gist.github.com/andyburke/1498758 (not using XMLHttpRequest). I then put that string as the "source" parameter in the post. I keep getting a "(#324) Requires upload file". It works perfectly if I just put in a url for the image. Can anybody help? Thanks!

// The function to post the image:
FB.api("/me/photos", 
          "POST",
          {
              "source": window.image_source,
              "message": "...",
              "fileName": "..."
          },
          function(response) {
            console.log(response.error);
            }
      });

// How I got the image_source:
var img = canvas.toDataURL("image/jpeg");
var encoded = img.substring(img.indexOf(',')+1,img.length);
var decoded = decode(encoded);
convertToFormData(decoded);

function convertToFormData(imageData) {
    var boundary = 'myboundary';

    var formData = "Content-Type: multipart/form-data; boundary=" + boundary + '\r\n\r\n';
    formData += '--' + boundary + '\r\n';
    formData += 'Content-Disposition: form-data; name="source"; filename="' + "myfile" + '"\r\n';
    for ( var i = 0; i < imageData.length; ++i )
        formData += String.fromCharCode(imageData[i] & 0xff);
    formData += '\r\n';
    formData += '--' + boundary + '\r\n'+ 'Content-Disposition: form-data; name="message"\r\n\r\n';
    formData += "Description" + '\r\n';
    formData += '--' + boundary + '--\r\n';

    window.image_source = formData;
  }

function decode(input) {
    var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var lkey1 = keyStr.indexOf(input.charAt(input.length-1));    
    var lkey2 = keyStr.indexOf(input.charAt(input.length-2));    
    var bytes = (input.length/4) * 3;
    if (lkey1 == 64) bytes--; 
    if (lkey2 == 64) bytes--; 
    var chr1, chr2, chr3; var enc1, enc2, enc3, enc4;
    var i = 0;var j = 0;

    var uarray = new Uint8Array(bytes);
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    for (i=0; i<bytes; i+=3) {  
      enc1 = keyStr.indexOf(input.charAt(j++));
      enc2 = keyStr.indexOf(input.charAt(j++));
      enc3 = keyStr.indexOf(input.charAt(j++));
      enc4 = keyStr.indexOf(input.charAt(j++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      uarray[i] = chr1;     
      if (enc3 != 64) uarray[i+1] = chr2;
      if (enc4 != 64) uarray[i+2] = chr3;
    }
    return uarray;  
  }


推荐答案

你必须转动画布将图像转换为Blob以将其上传到Facebook:

You have to turn the Canvas Image into a Blob to upload it to Facebook:

const dataURItoBlob = (dataURI) => {
    let byteString = atob(dataURI.split(',')[1]);
    let ab = new ArrayBuffer(byteString.length);
    let ia = new Uint8Array(ab);
    for (let i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }
    return new Blob([ia], {
        type: 'image/jpeg'
    });
}

之后,您可以将博客与FormData一起使用:

After that, you can use the blog with FormData:

formData.append('source', blob);

资料来源: http://www.devils-heaven.com/facebook-javascript-sdk-photo-upload-from-canvas/

这篇关于Facebook Javascript表单数据照片上传:需要上传文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆