首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何通过JavaScript在Parse.com中保存图像?

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

Stack Overflow用户
提问于 2014-05-05 08:05:47
回答 2查看 7.7K关注 0票数 3

我想添加一个新的对象,在它的一个列中包含一个图像,但它没有保存我的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">
EN

回答 2

Stack Overflow用户

发布于 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();
   }
票数 8
EN

Stack Overflow用户

发布于 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);
    }
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23463012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档