如何在浏览器中使用superagent将字符串作为文件发布

时间:2015-07-16 00:44:31

标签: javascript superagent

我正在尝试将字符串作为文件从浏览器发布,如SO question

中所述

但我想用superagent来做这件事。我尝试了以下方法:

var request = require('superagent');
var boundary = "---------------------------7da24f2e50046";

var req = request.post('/api/items');    
req.part()
    .set('Content-Type', 'multipart/form-data; boundary='+boundary)
    .set('Content-Disposition', 'form-data; name="file"')
    .write('my-string')
    ;
req.end(function(err, response) {
    if(err) { console.err(err.status_code); }
    else { console.log(response.body); }
});

我得到的错误是:未捕获的TypeError:req.part不是函数

1 个答案:

答案 0 :(得分:1)

In superagent project, there are two files: ./lib/client.js(using in browser), ./lib/node/index.js(using in node). no part method in ./lib/client.js.