如何使用 javascript 显示 parse.com 数据库中的图像?

jav*_*ish 1 javascript jquery parse-platform

我正在寻找有关如何使用 javascript 从我的 Parse 数据库以 HTML 格式显示图像的答案。事实证明,阅读 parse.com 上的文档并没有什么帮助。

我存储图像的类称为“内容”。图像作为文件存储在名为“图像”的列中。

Dan*_*ank 5

来自解析 JavaScript 指南

如何最好地检索文件内容取决于您的应用程序的上下文。由于跨域请求问题,最好让浏览器为您完成工作。通常,这意味着将文件的 URL 呈现到 DOM 中。在这里,我们使用 jQuery 在页面上渲染上传的个人资料照片:

var profilePhoto = profile.get("photoFile");
$("profileImg")[0].src = profilePhoto.url();
Run Code Online (Sandbox Code Playgroud)

所以你的步骤可能是这样的:

  1. 查询包含所需图片的行的内容类
  2. 获取图片的网址
  3. 设置图像元素的来源

下面的一些示例代码:

    // Query the content class for rows where the image exists
    var Content = Parse.Object.extend("content");
    var query = new Parse.Query(Content);
    query.exists("image");
    query.find({
      success: function(results) {
        // If the query is successful, store each image URL in an array of image URL's
        imageURLs = [];
        for (var i = 0; i < results.length; i++) { 
          var object = results[i];
          imageURLs.push(object.get('image'));
        }
        // If the imageURLs array has items in it, set the src of an IMG element to the first URL in the array
        if(imageURLs.length > 0){
          $('#color').attr('src', imageURLs[0]);
        }
      },
      error: function(error) {
        // If the query is unsuccessful, report any errors
        alert("Error: " + error.code + " " + error.message);
      }
    });
Run Code Online (Sandbox Code Playgroud)