从url Parse Cloud Code下载图像

时间:2015-01-30 23:50:55

标签: javascript parse-platform cloud-code

我尝试使用Parse Cloud Code下载Facebook个人资料照片并保存,但是,重定向似乎无法正常工作 - 有人可以给我一些指导吗?谢谢!

Parse.Cloud.httpRequest({
    url: pictureURL,
    followRedirects: true
    }).then(function(httpResponse){
      var imgFile = new Parse.File(request.user.id+".jpg" , httpResponse.buffer);

      //the httpResponse does not resolve correctly

      return imgFile.save();

    }).then(function(){
        request.user.set(constants.kBPUserProfileImageKey, image);
        request.user.save();
        response.success("updated image");

    },function(error) {
          response.error("failed to save object");
    });

1 个答案:

答案 0 :(得分:0)

首先,我认为诺言并不支持Parse.Cloud.httpRequest。
请参阅api

这是我的代码

// callback(savedParseFile, error)
   download: function (url, filename, callback) {

      Parse.Cloud.httpRequest({
         url: url,
         followRedirects: true,
         success:function(httpResponse) {
            var parseFile = new Parse.File(filename , {base64: httpResponse.buffer.toString('base64', 0, httpResponse.buffer.length)});

            // parseFile.save().then(function() {
            //    callback(parseFile, null);
            // }, function(error) {
            //    console.error('Downloader fails to save parse file.');
            // });

            parseFile.save({
               success:function() {
                  callback(parseFile, null);
               }, error:function() {
                  callback(null, 'Downloader fails to save parse file.');
               }
            });
         },
         error:function(httpResponse) {
            callback(null, 'Downloader fails to download file.');
         }
      });
   }