gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:42:07 28 4
gpt4 key购买 nike

所以我试图在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com