javascript - 从 URL 中获取图像 byte/base64

标签 javascript parse-platform base64 arrays

我正在寻找一种解决方案,我可以在其中提供特定图像的 URL,然后使用 Javascript 检索该图像的数据。我已经发现我不能直接拉取文件,所以字节数组或 base64 可以吗?

在有人对这个问题投反对票之前要具体一点:我想解析 URL 并以任何形式将图像发送到我的服务器。有很多类似的问题,但没有一个能回答这个问题。

我需要它做什么?我可以访问 API,其中我还提供了图像 url,但我希望通过 Parse.com 服务(类似于 CRON 作业)中的 Background Job 将它们上传到我的服务器。我知道如何使用 ParseObject 链接文件,但找不到如何将图像直接下载到 ParseCloud 并链接它的解决方案。

var Image = require("parse-image");
Parse.Cloud.job("getImagesJob", function(request, status) {
  Parse.Cloud.useMasterKey();
  var beerObj = Parse.Object.extend("My_class");
  var query = new Parse.Query(beerObj);
  query.first().then(function(objs) {
  var brew = objs.get("brewery");
  var bname = objs.get("beer_name");
  //var fullName = brew + " " + bname;
  var fullName = "Browar Name";
  console.log(fullName);

    return Parse.Cloud.httpRequest({
      method: 'GET',
      url: 'api server address',
      headers: {
        'Content-Type': 'application/json;charset=utf-8'
    },
      params: {
        q : fullName,
        client_id : '...',
        client_secret : '...'
    }
    }).then(function(httpResponse) {
        var json = JSON.parse(httpResponse.text);
        if(json.meta.code === 200){
            if(json.response.beers.count > 0){

                if(json.response.beers.items[0].beer.beer_label === "/default.png"){
                    console.log("brak etykiety dla " + fullName);

                } else {
                    console.log("znaleziono etykietę dla " + fullName);
                    Parse.Cloud.httpRequest({ //NOT REACHING
                      url: json.response.beers.items[0].beer.beer_label,
                      success: function(response) {
                        // The file contents are in response.buffer.
                        var image = new Image();
                        return image.setData(response.buffer, {
                          success: function() {
                              objs.set("logo", image.data());
                            console.log("udalo sie dolaczyc");
                          },
                          error: function(error) {
                            // The image data was invalid.
                            console.error(error);
                          }
                        })
                      },
                      error: function(error) {
                        // The networking request failed.
                      }
                    });

                }

            } else {
                // daj cokolwiek żeby się nie zacięło na jednym

                console.log("Brak danych dla piwa: " + fullName);
            }       
        }

    }, function(httpResponse) {
      console.error('Request failed with response code ' + httpResponse.status + httpResponse.text);
    });

}).then(function(obj) {
  status.success("Zrobione");
}, function(error) {
  status.error(error);
});
});

最佳答案

您可以在 Cloud Code 中使用 parse-image 模块,就像在他们的 documentation 中一样。

var Image = require("parse-image");

Parse.Cloud.httpRequest({
  url: YOUR_URL,
  success: function(response) {
    // The file contents are in response.buffer.
    var image = new Image();
    return image.setData(response.buffer, {
      success: function() {
        console.log("Image is " + image.width() + "x" + image.height() + ".");
      },
      error: function(error) {
        // The image data was invalid.
      }
    })
  },
  error: function(error) {
    // The networking request failed.
  }
});

通过上面的代码,您可以使用image.data() 获取Buffer 中的图像数据。要获取 base64 数据,请使用 image.data().toString("base64")

关于javascript - 从 URL 中获取图像 byte/base64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33713628/

相关文章:

javascript - 使用不同的单击事件后,jQuery 单击事件未触发

ios - 数据库中的组名。仅显示一组名称 UITableView 行

unit-testing - 是否有可用于完全测试 base64 编码/解码器的数据集?

javascript - 如何在 JavaScript 中添加新元素?

javascript - 查找 div 内中心点和中点的 x 和 y 坐标

javascript - Parse.com : How to paginate results from a Parse. 查询?

ios - 使用 PFLogInViewController 创建 PFUser 后,Parse.com iOS 用户电子邮件打开可读

android - 为 URI 使用编码图像

java - 在 Freemarker 模板中编码 base64

javascript - 横幅 javascript 变得更快