如何通过JavaScript在Parse.com中保存图像?

如何通过JavaScript在Parse.com中保存图像?,javascript,image,parse-platform,Javascript,Image,Parse Platform,我想添加一个新的对象,其中一列包含一个图像,但它不保存我的图片,我的代码中有错误吗?特别是保存图像的一部分 出现问题的JavaScript代码: 它从来没有上传我的图片解析 <script type="text/javascript"> Parse.initialize("key", "key"); var products = Parse.Object.extend("products"); var fileUploadControl = $("

我想添加一个新的对象,其中一列包含一个图像,但它不保存我的图片,我的代码中有错误吗?特别是保存图像的一部分

出现问题的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>

初始化(“key”,“key”);
var products=Parse.Object.extend(“产品”);
var fileUploadControl=$(“#profilePhotoFileUpload”)[0];
如果(fileUploadControl.files.length>0){
var file=fileUploadControl.files[0];
var name=“photo.png”;
var parseFile=new Parse.File(名称,文件);
}
parseFile.save().then(函数()){
//文件已保存以进行分析。
},函数(错误){
//无法读取该文件,或者无法保存以进行分析。
});
在这里,我添加了html行以上载文件:

<input type="file" id="profilePhotoFileUpload">

检查(在该部分末尾,就在关于检索文件的部分之前)。基本上,问题在于,与任何其他解析对象一样,您需要先保存它,然后在保存完成后才能使用它

创建文件,保存它,然后在save
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);
    }
});

我得到了答案我正在和你分享也许有人会受益

要上载文件的
THML
行是:

<input type="file" id="pic">

@A.Wolff你有什么建议吗?@handomeguy你有什么建议吗?这个问题是在根据代码发布答案后编辑的,Timothy的答案仍然正确:首先保存你的解析文件,然后(在该承诺的回调中)保存你的对象谢谢你的回答,但我仍在痛苦地思考如何做,我遵循了解析、com指南,但它仍然无法成功运行。。。如果你有一个示例代码,我将非常感谢。哦,我想编辑我的代码,但我在回答我的问题时做了错误,我不能删除,但如果你能发现我的错误,我将非常感激。。它运行但不保存任何文件非常感谢,我得到了答案并将其作为答案发布在这里,感谢您的帮助@TimothyWaltersproduct save需要在parseFile.save result函数中
     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();
   }