2013-08-31 107 views

回答

4

Parse JavaScript Guide

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

var profilePhoto = profile.get("photoFile"); 
$("profileImg")[0].src = profilePhoto.url(); 

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

  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); 
     } 
    }); 
+0

谢谢丹尼尔。 它似乎已经成功地查询数据库(当我查看Chrome中的Developer Tools中的网络标签时,我看到'内容'是其中一个请求)。 但是,我仍然不知道如何显示图像。我该如何将IMG元素的src放在我的身上? – javinladish

+0

我可以在HTML中看到IMG元素的外观吗?如果我知道它的id/class是什么,我可以相应地更新我的示例代码。你也知道你的查询返回多少个项目吗?您可以尝试在Parse的数据浏览器中查询,方法是转到https://parse.com/apps并单击相应应用程序的数据浏览器链接。 –

+0

嗨丹尼尔,谢谢你的回复。举例来说,让我们说图像的ID是“颜色”。所以HTML就像''。如果我想显示数据库中的所有图像怎么办?目前为止,我的数据浏览器中共有8个项目。 – javinladish

0

我碰到了同样的问题,但我还是决定将图片上传到自己的服务器和存储在数据库中的图像的URL。希望有所帮助。

1

建立在上面接受的答案上。以下是将多个图像重新放回页面的示例。

var GlobalBadges = Parse.Object.extend("GBadges"); 
    var query = new Parse.Query(GlobalBadges); 
    query.exists("Global_Badge_Name"); 
    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('Global_Badge_Name')); 
     } 
     // If the imageURLs array has items in it, set the src of an IMG element to the first URL in the array 
     for(var j = 0; j < imageURLs.length; j++){ 
      $('#imgs').append("<img src='" + imageURLs[j] + "'/>");     
     } 
     }, 
     error: function(error) { 
     // If the query is unsuccessful, report any errors 
     alert("Error: " + error.code + " " + error.message); 
     } 
    }); 

</script> 

<div id="imgs"> </div> 
</body> 
</html> 

而这个例子说明你必须拉多个图像并在div中定义它们。

var GlobalBadges = Parse.Object.extend("GBadges"); 
    var query = new Parse.Query(GlobalBadges); 
    query.exists("Global_Badge_Name"); 
    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('Global_Badge_Name')); 
     } 

     $('#Image01').attr('src',imageURLs[0]); //first image 
     $('#Image02').attr('src',imageURLs[1]); //second image 
     $('#Image03').attr('src',imageURLs[2]); //third image 
     }, 
     error: function(error) { 
     // If the query is unsuccessful, report any errors 
     alert("Error: " + error.code + " " + error.message); 
     } 
    }); 
+1

为了让效率更高一点,我认为你可以将这些var声明移到你的循环之外。例如。 var obj; for(var i ...){obj = results [i]; } – graemeboy

相关问题