作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我试图在 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/
我是一名优秀的程序员,十分优秀!