javascript - 将图像上传到解析帐户

标签 javascript html image upload parse-platform

所以我试图在 Parse 的用户类中上传这张图片。首先我从我的电脑上传图片然后按提交将它发送/保存在类(class)的图片栏中(这是文件类型):

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("img", x) 行收到以下错误; :

未捕获的 InvalidStateError:无法从中读取“selectionDirection”属性

'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

最佳答案

在将文件发送到数据库之前,您应该创建一个新的 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);
  }
});

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

关于javascript - 将图像上传到解析帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25341915/

相关文章:

html - 表格单元格的左边框在 Firefox 和 IE 中不通过

java - 无法从 byte[] 读取 JPEG 文件

ios - SQLite 保存和加载图像 iOS

html - 为什么我的 Logo 旁边有一条红线?

javascript - 更新仅发生在最后一行,而不是第一行

javascript - 使用javascript动态更新div的宽度

javascript - '类型错误 : NetworkError when attempting to fetch resource' on form submit ReactJS

Javascript (jQuery) 效果等待图像加载

javascript - Gulp-file-include 不适用于 gulp.watch

jquery - CSS cubic-bezier 过渡导致下面的内容跳转