2016-09-20 141 views
-1

如何在發送node.js的(多)通過http上傳的文件,像這樣:上傳文件在春季啓動

var options = { 
    host: url, 
    port: 8080, 
    path: '/sendFile', 
    method: 'POST' 
}; 

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); 
    }); 
}).end(); 

在HTTP請求時,它是如何可以發送文件以多部分形式上傳?

回答

0

這取決於。有多種策略。

最簡單的是通過使用.FORM()功能從申請到使用正確的,sonmething像:

var form = req.form(); 
form.append('file', file_content, { 
filename: 'myfile.ext', 
contentType: 'corresponding content/type' 
}); 

,然後執行POST請求

...或者你可以流呢並讓請求提取所需的數據

form.append('file', fs.createReadStream('path/to/file'));