0
쓰기 :Node.js를 & 익스프레스/읽기 스트림 I는 서버 측에서 다음 코드가 클라이언트
client.getFile(url.substring(1, url.length), function (err, res) {
var outputStream1 = fs.createWriteStream("./tmp/" + url);
res.on('data', function (chunk) {
outputStream1.write(chunk);
});
res.on('end', function() {
outputStream1.end();
// I got the response in the parameters function. It refer to the res express param
response.setHeader("content-type", 'application/octet-stream;charset=utf-8');
response.setHeader("content-type", 'application/octet-stream;charset=utf-8');
response.setHeader("Content-Disposition", 'attachment; filename="' + fileName + '"' + "; filename*=UTF-8''" + fileName + "");
var readStream = fs.createReadStream("./tmp/" + url);
fs.stat("./tmp/" + url, function(error, stat) {
console.log("The size of the file is: " + stat.size);
})
readStream.on('open', function() {
readStream.pipe(response);
});
readStream.on('end', function() {
response.end();
});
을 출력은 다음과 같습니다
The size of the file is: 5992213
는 지금은 서버에서 요청을 보낸 클라이언트 및 파일로 저장하려면, 그래서 클라이언트 측에서 다음을 수행하십시오
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
var writeStream = fs.createWriteStream("C://Client//create.pdf");
fs.stat("C://ClientFile//something5.pdf", function(error, stat) {
res.pipe(writeStream);
});
하지만 저장 파일이 10.4 MB
것을보고, 내가 그것을 열 때, 일 e 다음 오류가 표시됩니다 : "파일이 부족합니다."
내가 뭘 잘못 했니? 라우터에서
귀하의 솔루션은 생각보다 과장되어 있습니다. 클라이언트 사이트에서 파일을 다운로드하려면 http://expressjs.com/4x/api.html#res.download – cieszynka
@cieszynka : 'TypeError : Object #에'download'' 메서드가 없습니다라고 말하십시오. –