如何在node.js中使用POST方法传输文件?

时间:2016-04-15 13:43:54

标签: node.js

我想使用POST方法传输图像

var queryString = querystring.stringify({
    "category": "1",
    "photo": "/home/dmitriy/image2.jpg" // HOW I CAN TO TRANSFER THIS IMAGE?
});

var options = {
    hostname: 'dimonchikone.ucoz.net',
    port: 80,
    path: '/uapi/photo',
    method: method,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': queryString.length
    }
};

var req = http.request(options, function(res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });
});

req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
});

// write data to request body
req.write(queryString);
req.end();

但不明白怎么样?在php中,当我使用curl时,我只需编写下一个代码

"photo": "@/home/dmitriy/image2.jpg"

我如何在node.js中传输此文件?我想要传输图像,然后使用PHP $ _FILE

1 个答案:

答案 0 :(得分:1)

我建议使用public TreeModel GetParent(Func<TreeModel, bool> function) { return GetParent(Where(function)); } private TreeModel GetParent(TreeModel treeModel) { if (Children == null) return null; if (Children.Contains(treeModel)) return this; foreach (TreeModel child in Children) { TreeModel result = child.GetParent(treeModel); if (result != null) return result; } return null; } private TreeModel Where(Func<TreeModel, bool> function) { if (Children == null) return null; foreach (TreeModel child in Children) { if (function(child)) return child; TreeModel result = child.Where(function); if (result != null) return result; } return null; } module,它变得微不足道了:

request