2015-05-01 28 views
1

有誰知道如何讀取或訪問HTTP響應的附件?NodeJS從HTTP響應中讀取文件附件

我的意思是,服務器的響應是:

HTTP/1.1 200 OK 
Server: something 
Date: Fri, 01 May 2015 10:22:29 GMT 
Content-Type: application/pdf 
Transfer-Encoding: chunked 
Connection: keep-alive 
Vary: Accept-Language, Cookie 
X-Frame-Options: SAMEORIGIN 
Content-Language: en 
Content-Disposition: attachment; filename=mybill-234324.pdf 
Set-Cookie: s=xxxxx; expires=Fri, 15-May-2015 10:22:29 GMT; httponly; Max-Age=1209600; Path=/; secure 
Strict-Transport-Security: max-age=15552000 

但是這是哪裏mybill-234324.pdf文件內容???

我的代碼:

var req = https.request({ 
    method: 'GET', 
    host: 'thehost.ext', 
    path: '/account/bills/' + number + '/', 
    headers: { 
     'Cookie': 's=supercryptedhash;', 
     'Accept-Language': 'en-US,en' 
    } 
    }, function(res) { 
    // ???? 
    }); 

    req.on('error', function(err) { 
    console.error('Request Error', err); 
    }); 

    req.end(); 
+0

提供的代碼示例,請在問題編輯提供 – vanadium23

+0

。 –

+0

嘗試看到答案:http://stackoverflow.com/questions/20132064/node-js-download-file-using-content-disposition-as-filename – vanadium23

回答

0

https文檔(https://nodejs.org/api/https.html)提供了一個例子:

options = { ... }; 

var req = https.request(options, function(res) { 
    console.log("statusCode: ", res.statusCode); 
    console.log("headers: ", res.headers); 

    res.on('data', function(data) { 
     // var `data` is a chunk of data from the response body 
     process.stdout.write(data); 
    }); 
}); 


req.on('error', function(e) { 
    console.error(e); 
}); 

req.end(); 
+0

我檢查了這一點,但當Content-Disposition標題爲「附件」時,它沒有奏效......讓我再試一次,看看。 –

+0

它應該仍然有效,'http'和'https'模塊不會特別關注'content-disposition'標頭。 – Gabriel

+0

Gabriel是什麼意思?在這種情況下我不能下載,或者在閱讀響應體時應該在這裏下載? –