4

我正在使用来自 Angular js 应用程序的 parse.com 的 js api。我正在尝试保存/更新用户的个人资料图片。我有以下代码:

一些 html 。. .

<input type="file" capture="camera" accept="image/*" id="profilePhotoFileUpload">

感谢google i/oraymond解析 js 指南

我的控制器中的代码:

$scope.updateUserProfile = function (user) {

        var fileUploadControl = $("#profilePhotoFileUpload")[0];
        if (fileUploadControl.files.length > 0) {
            var file = fileUploadControl.files[0];
            var name = "photo.jpg";

            var parseFile = new Parse.File(name, file);

            parseFile.save();


}

我不断收到此错误:

TypeError:无法在 Object.Parse.File.save 调用未定义的方法“then”(parse-1.2.8.js:4084:43)

在此处输入图像描述

调用 parseFile.save() 时

基本上 _source 是未定义的。. . 为什么?!

谢谢!

4

2 回答 2

1

我终于解决了这个问题,因为我的最终目标是将它与phonegap一起使用,以及这篇文章中的信息。. 非常感谢雷蒙德卡姆登!

函数得到图片(数据){

window.resolveLocalFileSystemURI(data, function(entry) {

    var reader = new FileReader();

    reader.onloadend = function(evt) {
        var byteArray = new Uint8Array(evt.target.result);
        var output = new Array( byteArray.length );
        var i = 0;
        var n = output.length;
        while( i < n ) {
            output[i] = byteArray[i];
            i++;
        }                
        var parseFile = new Parse.File("mypic.jpg", output);

        parseFile.save().then(function(ob) {
                navigator.notification.alert("Got it!", null);
                console.log(JSON.stringify(ob));
            }, function(error) {
                console.log("Error");
                console.log(error);
            });

    }

    reader.onerror = function(evt) {
          console.log('read error');
          console.log(JSON.stringify(evt));
      }

    entry.file(function(s) {
        reader.readAsArrayBuffer(s);
    }, function(e) {
        console.log('ee');
    });

});

}

于 2013-07-11T13:50:20.353 回答
1

如果其他人正在使用 Phonegap 和 Parse,这是 Raymond Camden 的另一个有用示例,它拍了一张照片:

http://www.raymondcamden.com/2013/07/23/better-example-of-phonegap-parse-and-uploading-files

var imagedata = "";    

$("#takePicBtn").on("click", function(e) {
    e.preventDefault();
    navigator.camera.getPicture(gotPic, failHandler, 
        {quality:50, destinationType:navigator.camera.DestinationType.DATA_URL,
         sourceType:navigator.camera.PictureSourceType.PHOTOLIBRARY});
});

function gotPic(data) {
    console.log('got here');
    imagedata = data;
    $("#takePicBtn").text("Picture Taken!").button("refresh");
}

它被保存为:

var parseFile = new Parse.File("mypic.jpg", {base64:imagedata});
        console.log(parseFile);
            parseFile.save().then(function() {
                var note = new NoteOb();
                note.set("text",noteText);
                note.set("picture",parseFile);
                note.save(null, {
                    success:function(ob) {
                        $.mobile.changePage("#home");
                    }, error:function(e) {
                        console.log("Oh crap", e);
                    }
                });
                cleanUp();
            }, function(error) {
                console.log("Error");
                console.log(error);
            });

特别注意{base64:imagedata}, 因为这是使用这样的字符串数据创建解析文件的关键。

于 2015-02-17T07:21:41.903 回答