簡體   English   中英

parse.com和Java腳本-為用戶上傳圖片並顯示圖片

[英]parse.com & java script- upload image for user and show the image

我建立了一個注冊表單,那里有用於用戶圖像的文件

<input type="file" name="img1"   id="img1">

現在,當用戶按下“ signup”按鈕時,我將其稱為signUp函數:

  function signUp(){

  var username = document.getElementById('username_signUp').value;     
 var password = document.getElementById('password_signUp').value;


Parse.initialize("kkbFC-----dldRYvUOywO8", "2ux4CkBgv4QB---wNguk");


 var user = new Parse.User();
 user.set("username", username);
 user.set("email", username);
 user.set("password", password);


var fileUploadControl = $("#img1")[0];
if (fileUploadControl.files.length > 0) {
  var file = fileUploadControl.files[0];
  var name = "photo.png";

  var parseFile = new Parse.File(name, file);
  parseFile.save().then(function() {
  // The file has been saved to Parse.
}, function(error) {
        alert("Error: " + error.code + " " + error.message);

  // The file either could not be read, or could not be saved to Parse.
});



user.set("image", file);

}

user.signUp(null, {
    success: function (user) {

    },
    error: function (user, error) {
    }
});  


}

注冊完成,我在parse.com上獲得新的一行。 在我提出的“圖像”中

{"lastModifiedDate":{"__type":"Date","iso":"2014-01-18T20:07:11.000Z"},"name":"empty-f.gif","size":2498,"type":"image/gif","webkitRelativePath":""}

我並不滿意它的好壞,但是當我嘗試獲得圖像時-它似乎是不確定的。

alert(profilePhoto.url); ->It show me the message "undefined"

謝謝!

//Parse's asynchronous nature causes some of hard-to-spot errors. 
//The right way to use parse is to keep nesting code into the 
//success handlers/promise handler.
function signUp(){
  var username = document.getElementById('username_signUp').value;     
  var password = document.getElementById('password_signUp').value;
  Parse.initialize("kkbFCxNGrHeUB7MpVEIRGMvZYgh0dldRYvUOywO8", "2ux4CkBgv4QBNYwlLh7RGmNQjXh7t0x7jGjwNguk");

  var user = new Parse.User();
  user.set("username", username);

  //THE NEXT LINE SETS EMAIL TO BE THE USER NAME. THAT IS MOST LIKELY A BUG
  user.set("email", username);
  //-----------------------------------------------------------------------

  user.set("password", password);

  var fileUploadControl = $("#img1")[0];

  if (fileUploadControl.files.length > 0) {
    var file = fileUploadControl.files[0];
    var name = "photo.png";
    var parseFile = new Parse.File(name, file);
    parseFile.save().then(function(parseFile) {
      // The file has been saved to Parse. file's URL is only available 
      //after you save the file or after you get the file from a Parse.Object.
      //Get the function url() on the Parse.File object.
      var url = parseFile.url();
      user.set("image", url);
      user.signUp();
      }, 
      function(error) {
        // The file either could not be read, or could not be saved to Parse.
        alert("Error: " + error.code + " " + error.message);
      });
  };
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM