imgur图片上传不适用于base64数据?

时间:2013-12-11 07:20:03

标签: javascript base64 imgur

我已经尝试了超过3个小时才能接受基本的64位数据,但绝对没有用,我已经确认数据是正确的,我已经确认我可以使用网址图片发送到imgur,我无法让它发挥作用。我正在使用的代码如下:

$(document).ready(function(){
            function readImage(input) {
                var FR= new FileReader();
                FR.onload = function(e) {
                    console.log(e.target.result);
                    $.ajax({
                                url: 'https://api.imgur.com/3/image',
                                type: 'post',
                                headers: {
                                    Authorization: 'Client-ID 40dbfe0cfea73a7'
                                },
                                data: {
                                    image: e.target.result
                                },
                                dataType: 'json',
                                success: function(json) {
                                    console.log(json);
                                },
                                error: function(json) {
                                    console.log(json);
                                }
                            });
                };       
                FR.readAsDataURL( input );
            }
    });

input是我从dropzone.js获取的表单输入,base64数据的一个示例是:

  

数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAANgAAADYCAIAAAAGQrq6AAAABnRSTlMA / gABAP1bbA07AAAD + klEQVR4nO3dMW4sNxBAQa3h + 19ZDhwzoNE0H / dXpRJGo90HBg3O8PP7 + / SDT / 11 + wbg50eIRAiRBCGSIEQShEiCEEn4e / WDZ + fzf97Hf7aag9buf3deu7r / V / 7fldX9WxFJECIJQiRBiCQIkQQhkiBEEpZzxJVb + xd352Sv3Odpr3wOVkQShEiCEEkQIglCJEGIJAiRhO054srU / Gxq7nV67rh7 / dp8sfZ9WRFJECIJQiRBiCQIkQQhkiBEEsbmiDWnn / + dur73U / 7LikiCEEkQIglCJEGIJAiRBCGS8LVzxF277yN8 / E / WWBFJECIJQiRBiCQIkQQhkiBEEsbmiLW519Q5Jbee / z09X6x9X1ZEEoRIghBJECIJQiRBiCQIkYTtOWLtPX + n7c4dp35 / yivflxWRBCGSIEQShEiCEEkQIglCJOFT25d2y + k5X + 1ckxorIglCJEGIJAiRBCGSIEQShEjCcj9ibb / d1PklU + csn36 + eGruOHX / p3uwIpIgRBKESIIQSRAiCUIkQYgkLOeItedta + 81rM3npty6HysiCUIkQYgkCJEEIZIgRBKESMLxc1ZunXdy + n523br + yul9mbvXtyKSIEQShEiCEEkQIglCJEGIJGy / H / H0PryVV / b53Zpfnn5 + / PT + VCsiCUIkQYgkCJEEIZIgRBKESMK1c1Zemaut1Oapr38 + VkQShEiCEEkQIglCJEGIJAiRhLHnmm + dR7JSm6tNOf1 + xF3ej8hXESIJQiRBiCQIkQQhkiBEErbPa145fT7y7t + dev9i7bzp3eu / SJ / SikiCEEkQIglCJEGIJAiRBCGSMHZe89RzyqfnXiu3zkd + 5VzmXbvXsSKSIEQShEiCEEkQIglCJEGIJBzfj7j7 + 6 + FL / Ktzx3vXt8 ckScJkQQhkiBEEoRIghBJECIJY / sRp9x6jvi0W / v8VqaeN5 / ab2pFJEGIJAiRBCGSIEQShEiCEEkY2494y + l9kLvzsFfOvz59nV1WRBKESIIQSRAiCUIkQYgkCJGE7fOaa3Oyldo50affg / JK / 7tiRSRBiCQIkQQhkiBEEoRIghBJ2J4jrtTmYSunz48 + RFA + Q88181WESIIQSRAiCUIkQYgkCJGEsTnitzp93vSt82ZOz1PtR + RJQiRBiCQIkQQhkiBEEoRIwtfOEW + 9529q3nb6OeiVqXmk / Yg8SYgkCJEEIZIgRBKESIIQSRibI956b + LKrXONp + Z / p89NmTL1OVsRSRAiCUIkQYgkCJEEIZIgRBK254i19wWuTJ13UnteePc6t85NsR + RJwmRBCGSIEQShEiCEEkQIgmf2j5C / kxWRBKESIIQSRAiCUIkQYgkCJGEfwDA94Tqvnz2uQAAAABJRU5ErkJggg ==

我收到400错误,这意味着(根据docs A)我要省略参数,或使用无效值。或B)图像损坏或格式无效。 这对我来说没有意义,因为将base64数据复制到浏览器中工作正常,授权工作正常,而且我知道如果图像是网址我可以上传。所以我不确定这里发生了什么!如果您需要更多数据,请告诉我,我会尽快更新此问题!非常感谢你们!

1 个答案:

答案 0 :(得分:10)

你必须用空字符串替换“data:image / png; base64”,引用(在这里添加JSFiddle addres因为SO不允许没有代码的小提琴链接)jasdeepkhalsa / L5HmW /或Save base64-encoded image with AJAX request in PHP