固定的。的NodeJS。 Stream.pipe(响应)不会创建下载

时间:2016-04-12 19:49:03

标签: javascript angularjs node.js

I found this solution, but for me doesn't work.

嗨,我试图从服务器端上传文件(我刚刚使用fs.writefile创建),我想得到文件Side(Angular js)。我可以从服务器获取数据,但我的Stream.pipe没有启动下载。

这是后端代码的示例

    params.fs.writeFile(pathFile,response, function (err) {
                                if (err) {
                                    throw err;                           
                                }
                                res.writeHead(200,{
                                    'Content-Type': 'text/xml',
                                    'Content-Disposition': 'attachment; filename=' + fileName

                                });
                                var fileStream = params.fs.createReadStream(pathFile);
                                console.log(fileStream);
                                fileStream.pipe(res);` 

and here frontend `Class.getXmlFile(id).then(function(xmlFile){ console.log(xmlFile);`:


Thanks.

So I am going to write how I fixed this: After make my function in server I am sending already my file to front end, so I only need a normal request instead XmlHttpRequest that is waiting for a promise(response) to ask my file in server:

Here both codes:

params.fs.writeFile(pathFile,response, function (err) {
                            if (err) {
                                throw err;                           
                            }
                            res.writeHead(200,{
                                'Content-Type': 'text/xml',
                                'Content-Disposition': 'attachment; filename=' + fileName

                            });
                            var fileStream = params.fs.createReadStream(pathFile);
                            console.log(fileStream);
                            fileStream.pipe(res);

客户端:window.open(path / to / backend)

0 个答案:

没有答案