parse.com& java脚本 - 为用户上传图像并显示图像 [英] parse.com & java script- upload image for user and show the image

查看:99
本文介绍了parse.com& java脚本 - 为用户上传图像并显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个注册表单,在那里提交用户图像

I build a signup form and there, there is filed for the user image

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

现在,当用户按下注册按钮时,我会调用函数signUp:

Now , When the user press the button "signup" I call the function 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) {
    }
});  


}

signUp完成后我得到新的一行在parse.com上。
在图像字段中,我保存了


The signUp complete and I get new row on parse.com. In the "image" filed I habe

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

如果不好或不好,图像 - 它似乎是未定义的。

I don't undersatd if it is good or not, but when I try to get the image - it seems to be undfined.

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

这篇关于parse.com&amp; java脚本 - 为用户上传图像并显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆