2013-08-31 108 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

相關問題