2014-05-05 114 views
2

我想添加一个新的对象,其中包含一列图像,但它不会保存我的图片,我的代码中是否有任何错误?特别是保存图像的一部分!如何通过JavaScript将图像保存在Parse.com中?

我的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"> 
+0

@ A.Wolff你有什么建议吗? – user3602314

+0

@Handsomeguy你有什么建议吗? – user3602314

+0

这个问题是根据代码发布回答后编辑的,Timothy的答案依然正确:先保存你的Parse文件,然后(在该Promise的回调中)保存你的对象 – Sandra

回答

2

检查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); 
    } 
}); 
+0

感谢您的回答,但我仍然忍受着如何做它,我跟着解析,com指南,但它仍然不能成功工作...如果你有一个示例代码,我会感谢全部 – user3602314

+0

oooh我想编辑我的代码,但错误我做你的回答我我不能删除,但如果你可以小鸡它发现我的错误,我会感激..它的运行,但不保存任何文件 – user3602314

+0

非常感谢,我得到了答案,并在这里发布它作为答案,谢谢你的帮助@TimothyWalters – user3602314

7

我有我与您分享它的答案也许有人得利

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(); 
    } 
+2

产品保存需要*在parseFile.save结果函数中 – Sandra