将图像上传到解析帐户

时间:2014-08-16 16:59:36

标签: javascript html image upload parse-platform

所以我试图在Parse的用户类中上传这个图像。首先,我从计算机上传图像,然后按提交将其发送/保存在类的图像列(属于文件类型)中:

document.write('Profile Picture: ');
    var x = document.createElement("INPUT");
    x.setAttribute("type", "file");
    document.body.appendChild(x);
    document.write('<br>');

    var button = document.createElement('button');
    button.innerHTML = 'Submit';
    button.onclick = function(){
        var user = new Parse.User();
        console.log(x);
        user.set("img", x);      
        user.save(null, {
          success: function(user) {
            alert('New user added!');
          },
          error: function(user, error) {
            // Show the error message somewhere and let the user try again.
            alert("Error: " + error.code + " " + error.message);
          }
        });
    };
    document.body.appendChild(button);

我在user.set行上收到以下错误(&#34; img&#34;,x); :

未捕获的InvalidStateError:无法读取&#39; selectionDirection&#39;物业来自

'HTMLInputElement': The input element's type ('file') does not support selection. parse-1.2.19.min.js:1
(anonymous function) parse-1.2.19.min.js:1
w.each.w.forEach parse-1.2.19.min.js:1
t._objectEach.t._each parse-1.2.19.min.js:1
t._decode parse-1.2.19.min.js:1
n.extend.set parse-1.2.19.min.js:2
button.onclick

1 个答案:

答案 0 :(得分:1)

在将文件发送到数据库之前,您应该创建一个新的Parse.File对象。要发送图像,以下代码适用于我。

var fileUploadControl = document.getElementById('profile_upload'); //  get a reference to your uploaded file



if (fileUploadControl.files.length > 0) {   //  check if valid input
  var file = fileUploadControl.files[0]; 
  var name = "photo.jpg";     //  save it in your cloud with this name, don't worry, you can have multiple files with same name

  var parseFile = new Parse.File(name, file);   //  create Parse.File object


}


user.set("img",parseFile);

user.signUp(null, {
  success: function(user) {
    alert("Done!");


  },
  error: function(user, error) {
    // Show the error message somewhere and let the user try again.
    alert("Error: " + error.code + " " + error.message);
  }
});

我添加了一些评论来帮助你。