素材牛VIP会员
云代码nodejs中,请问如何将一个图片url存入AV.File?
 pu***wp  分类:Node.js  人气:604  回帖:2  发布于2年前 收藏

代码如下

var url = 'http://img.t.sinajs.cn/t5/style/images/global_nav/WB_logo.png?id=1382608648268';
AV.Cloud.httpRequest({
    url: url,
    text: false,
    success: function (httpResponse) {
        var filename = 'image.png';

        var file = new AV.File(filename, httpResponse.buffer);
//      var file = new AV.File(filename,[ 0xBE, 0xEF, 0xCA, 0xFE ]);

        file.save().then(function () {
            // The file has been saved to AV.
            res.send('OK');
        }, function (error) {
            res.send(500, error);
        });
    },
    error: function (httpResponse) {
        console.error('Request failed with response code ' + httpResponse.status);
        res.send(httpResponse.status);
    }
});
return;

如果用上面的代码,我会收到这样的错误:

Thu, 16 Jan 2014 09:27:02 GMT uncaughtException: Cannot call method 'then' of undefined
TypeError: Cannot call method 'then' of undefined
    at Object.AV.File.save (/usr/local/lib/node_modules/avoscloud-code/node_modules/avoscloud-sdk/lib/av.js:4147:43)
    at AV.Cloud.httpRequest.success (/Users/ph/Documents/iphone/aphree_server/cloud/admin.js:43:12)
    at /usr/local/lib/node_modules/avoscloud-code/node_modules/avoscloud-sdk/lib/av.js:3709:27
    at wrappedResolvedCallback (/usr/local/lib/node_modules/avoscloud-code/node_modules/avoscloud-sdk/lib/av.js:3635:38)
    at /usr/local/lib/node_modules/avoscloud-code/node_modules/avoscloud-sdk/lib/av.js:3586:26
    at Array.forEach (native)
    at Object._.each._.forEach [as _arrayEach] (/usr/local/lib/node_modules/avoscloud-code/node_modules/avoscloud-sdk/lib/av.js:95:11)
    at _.extend.resolve (/usr/local/lib/node_modules/avoscloud-code/node_modules/avoscloud-sdk/lib/av.js:3585:10)
    at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/avoscloud-code/lib/cloud_code.js:298:20)
    at IncomingMessage.EventEmitter.emit (events.js:117:20)

如果用注释中的代码,则不会遇到任何问题。

请问是我哪里搞错了吗?buffer不能直接用在AVFile的参数中?

 标签:leancloudnode.js

讨论这个帖子(2)垃圾回帖将一律封号处理……

Lv4 码徒
ji***ca JS工程师 2年前#1

Buffer 不能直接用。我们有一篇 blog 讲解了怎么在云代码中处理上传文件:

http://blog.avoscloud.com/blog/2013/11/23/zai-yun-dai-ma-zhong-chu-li-shang-chuan-wen-jian/

Lv5 码农
陈***6 软件测试工程师 2年前#2

现在可以直接用Buffer了。

 文明上网,理性发言!   😉 阿里云幸运券,戳我领取