【问题标题】:How to save an Image in Parse.com via JavaScript?如何通过 JavaScript 在 Parse.com 中保存图像?
【发布时间】:2014-05-05 00:05:47
【问题描述】:

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

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

【问题讨论】:

  • @A.Wolff 你有什么建议吗?
  • @Handsomeguy 你有什么建议吗?
  • 这个问题是在根据代码发布答案后编辑的,Timothy 的答案仍然正确:首先保存您的 Parse File,然后(在该 Promise 的回调中)保存您的对象

标签: javascript image parse-platform


【解决方案1】:

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

上传文件的THML 行是:

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

&lt;script&gt; 中获取和保存图像的代码是:

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

【讨论】:

  • 产品保存需要inside parseFile.save 结果函数
【解决方案2】:

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

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

【讨论】:

  • 感谢您的回答,但我仍然很难做到,我按照 parse,com 指南进行操作,但仍然无法成功...如果您有示例代码,我将不胜感激
  • 哦,我想编辑我的代码,但我在你的回答中做错了,我无法删除,但如果你能发现我的错误,我将不胜感激。它运行但不保存任何文件
  • 非常感谢,我得到了答案并将其发布在这里作为答案,感谢您的帮助@TimothyWalters
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-24
  • 2014-01-24
  • 1970-01-01
  • 2010-12-24
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多