【问题标题】:How to post an uploaded file with http?如何使用http发布上传的文件?
【发布时间】:2011-02-14 20:46:04
【问题描述】:

我将如何在快递应用程序中做到这一点?那是, 将文件发布到 Facebook:

curl -F 'access_token=xyz' \
    -F 'source=@file.png' \
    -F 'message=Caption for the photo' \
    https://graph.facebook.com/me/photos

我正在使用以下内容从 repo 中的示例上传文件:

app.post('/', function(req, res, next){
 req.form.complete(function(err, fields, files){
   if (err) {
     next(err);
   } else {
     console.log('\nuploaded %s to %s'
       ,  files.image.filename
       , files.image.path);
     res.redirect('back');
   }
 });
})

【问题讨论】:

    标签: node.js commonjs express


    【解决方案1】:

    看看request-module,它(几乎)太简单了:

    fs.readStream('file.png').pipe(request.post('http://graph.facebook.com/me/photos'))
    

    这将创建一个对给定 URL 的 POST 请求,并通过它流式传输 file.png。添加其余字段应该相当简单。

    【讨论】:

      【解决方案2】:

      您可以使用 Node.js 创建 HTTP 请求。请参阅文档中的以下示例:

      http://nodejs.org/docs/v0.4.0/api/http.html#http.request

      【讨论】:

        猜你喜欢
        • 2011-04-25
        • 1970-01-01
        • 1970-01-01
        • 2012-03-24
        • 2014-01-11
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 2018-10-26
        相关资源
        最近更新 更多