1

I'm looking for an answer on how to display an image in HTML from my Parse database using javascript. Reading through the docs on parse.com proved to be little help.

The class where I have the image stored is called 'content'. The images are stored as a file in a column called 'image'.

4

3 に答える 3

5

Parse JavaScript ガイドから:

ファイルの内容を取得する最適な方法は、アプリケーションのコンテキストによって異なります。クロスドメイン リクエストの問題があるため、ブラウザに作業を任せることができるのが最善です。通常、これはファイルの URL を DOM にレンダリングすることを意味します。ここでは、jQuery を使用してページにアップロードされたプロフィール写真をレンダリングします。

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

したがって、手順は次のようになります。

  1. 目的の画像がある行のコンテンツ クラスをクエリします。
  2. 画像の URL を取得する
  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);
      }
    });
于 2013-08-31T23:40:56.753 に答える
0

同じ問題に遭遇しましたが、画像をサーバーにアップロードし、画像の URL をデータベースに保存することにしました。それが役立つことを願っています。

于 2013-09-01T03:02:52.580 に答える