當我查詢這個問題時,我發現很多帖子,但他們都提到如何將文件從瀏覽器上傳到node.js服務器。我想從node.js代碼上傳文件到另一臺服務器。我試圖根據我有限的node.js知識來編寫它,但它不起作用。如何從node.js上傳文件
function (data) {
var reqdata = 'file='+data;
var request = http.request({
host : HOST_NAME,
port : HOST_PORT,
path : PATH,
method : 'POST',
headers : {
'Content-Type' : 'multipart/form-data',
'Content-Length' : reqdata.length
}
}, function (response) {
var data = '';
response.on('data', function(chunk) {
data += chunk.toString();
});
response.on('end', function() {
console.log(data);
});
});
request.write(reqdata+'\r\n\r\n');
request.end();
})
上述函數被其他生成數據的代碼調用。
我試着用curl -F「file = @ <filepath>」上傳相同的數據文件,上傳成功。但是我的代碼失敗了。服務器返回一個應用程序特定的錯誤,提示上傳的文件無效/損壞。
我收集了tcpdump數據並在wireshark中進行了分析。從我的node.js代碼發送的數據包缺少多部分數據所需的邊界。我在wireshark數據包中看到此消息
The multipart dissector could not find the required boundary parameter.
任何想法如何在node.js代碼中完成此操作?
我一直在嘗試類似的事情。今天晚些時候我會試一試你的建議。我發現這個節點模塊應該讓這個任務變得簡單。 https://github.com/mikeal/request但它還沒有很好地爲我的目的工作。 – Jayesh 2011-04-22 02:20:57