2016-04-15 52 views
-1

我想通過使用POST方法如何在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

我建議使用requestmodule,它變得微不足道:

fs.createReadStream('/home/dmitriy/image2.jpg').pipe(request.put('http://dimonchikone.ucoz.net/uapi/photo/image2.jpg')) 
相關問題