我在服务器端有下一个代码:
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
现在我将请求从服务器发送到客户端,并希望将其保存到文件中,所以我在CLIENT端执行下一步:
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
,当我打开它时,会显示下一个错误:"文件内存不足"
我做错了什么?
答案 0 :(得分:0)
在路由器中,设置url以下载文件
app.get('/url-to-file.pdf', callback);
然后,在回调函数中使用res.download,如下所示:
callback = function(req, res){
res.download('server/path/to/file');
}
您可以将其写为
app.get('/url-to-file.pdf', function(req, res){
res.download('server/path/to/file');
});
客户端站点上的只需运行url:domain / url-to-file.pdf
在此解决方案中,大多数问题都可以动态生成URL