【问题标题】:I am unable to convert http.get image into base 64我无法将 http.get 图像转换为 base 64
【发布时间】:2014-11-27 14:01:41
【问题描述】:
app.getImage = function() {

    var image = Meteor.http.get("https://turtlerock-discourse.global.ssl.fastly.net/user_avatar/talk.turtlerockstudios.com/takran/45/879.png", {

    });
    var prefix = "data:image/png;base64,";
    var imagebase64 = new Buffer(image.content, 'binary').toString('base64');
    imagebase64 = prefix + imagebase64;

    console.log(imagebase64);
    return imagebase64;
}

但我没有看到结果, 有什么帮助吗? 这是错误的虚拟文本。

【问题讨论】:

  • 是在客户端还是服务器上?
  • 我认为您遇到了与我在此答案中描述的相同的问题:stackoverflow.com/questions/25654965/…
  • 这是在服务器端,因为新的缓冲区在客户端不可用。

标签: node.js meteor


【解决方案1】:

纯 Meteor 解决方案:

    var response = HTTP.call('GET', url,{npmRequestOptions: { encoding: null }})

    var data = "data:" + response.headers["content-type"] + ";base64," + new Buffer(response.content).toString('base64');

【讨论】:

    【解决方案2】:

    这就是我解决此问题的方法。

    app.getImage = 函数(){

    var myrequest = request.defaults({ encoding: null });
    
    var fut = new Future(); 
    var options = {
        "url" : "https://any.domain.com/any.png"
    }
    myrequest.get(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var data = "data:" + response.headers["content-type"] + ";base64," + new Buffer(body).toString('base64');
            fut.return(data);
        }
        else
            fut.return(null);
    });
    
    return fut.wait();
    

    }

    我假设这个解决方案应该带有流星代码本身, 但事实并非如此, 我不得不使用nodejs的方式来做到这一点。

    我仍然会等待有人基于纯流星方式发布答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-24
      • 2018-03-23
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 2020-06-12
      • 2020-11-27
      • 2015-11-11
      相关资源
      最近更新 更多