我想添加一个新的对象,在它的一个列中包含一个图像,但它没有保存我的Pic,我的代码中有什么错误吗?保存图像的特殊部分!!
出现问题的我的JavaScript:
它从来没有上传我的图片解析!!
<script type="text/javascript">
Parse.initialize("key", "key");
var products = Parse.Object.extend("products");
var fileUploadControl = $("#profilePhotoFileUpload")[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) {
// The file either could not be read, or could not be saved to Parse.
});
</script>
我在这里添加了html行来上传文件:
<input type="file" id="profilePhotoFileUpload">
发布于 2014-05-11 08:43:11
我得到了答案,我正在与你分享,也许有人会从中受益
上传文件的THML
行是:
<input type="file" id="pic">
在<script>
中获取和保存解析图像的代码如下:
var fileUploadControl = $("#pic")[0];
if (fileUploadControl.files.length > 0) {
var file = fileUploadControl.files[0];
var name = "photo.png";
var parseFile = new Parse.File(name, file);
//put this inside if {
parseFile.save().then(function() {
// The file has been saved to Parse.
}, function(error) {
// The file either could not be read, or could not be saved to Parse.
});
// Be sure of ur parameters name
// prod is extend of my class in parse from this: var prod = new products();
prod.set("picture", parseFile);
prod.save();
}
发布于 2014-05-06 15:01:19
检查documentation here (在那一节的末尾,就在关于检索文件的那一节之前)。基本上,问题是像任何其他Parse对象一样,您需要先保存它,然后在保存完成后才能使用它。
创建文件,保存它,然后在保存success
处理程序中,您可以使用对该文件的引用来保存对象。
更新:下面是如何修复上面的代码:
Parse.initialize("key", "key");
var products = Parse.Object.extend("products");
var base64 = "V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE=";
var file = new Parse.File("mypic.png", { base64: base64 });
file.save({
success: function(file) {
alert('File saved, now saving product with file reference...');
var prod = new products();
// to fill the columns
prod.set("productID", 1337);
prod.set("price", 10);
//I guess it need some fixing here
prod.set("picture", file);
prod.set("productName", "shampoo");
prod.set("productDescribe", "200 ml");
prod.save(null, {
success: function(prod) {
// Execute any logic that should take place after the object is saved.
alert('New object created with objectId: ' + prod.id);
},
error: function(error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and description.
alert('Failed to create new object, with error code: ' + error.description);
}
});
},
error: function(error) {
alert('Failed to save file: ' + error.description);
}
});
https://stackoverflow.com/questions/23463012
复制相似问题