0

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

Hi, I'm trying to upload a file from server side (that i just created using fs.writefile) , and I would like to get the file Side(Angular js). I can get the data from server , but my Stream.pipe doesnt launch a download.

here is a sample of the code from backend

    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);

Client Side: window.open(path/to/backend)

Community
  • 1
  • 1
Angela Pat.
  • 37
  • 2
  • 9
  • As you can see by the defined response http headers, you need something like window.location.href="path/to/your/route" instead to save it with xhr. – Sombriks Apr 12 '16 at 20:02
  • Do you mean that i should do in angular, window.location.href= "PATH TO MY SERVER/FILE" .. I was thinking when I send the pipe I am sending an option to download the file too. Am I right? Thanks . – Angela Pat. Apr 12 '16 at 20:06
  • 1
    I mean: you are on angular, and in order to download this file, instead to do a xhr request you need to navigate to it. it's because you're streaming the file and setting headers to inform the navigator that it's a download (attachment). also, if you pipe but don't be able to catch it back when it finish, you'll have certain trouble to delete the file on node side. – Sombriks Apr 12 '16 at 20:12
  • Agree with @Sombriks. better to stream directly to the response rather than creating temporary file. – Steve H. Apr 12 '16 at 20:23
  • Totally agree with @Sombriks. It is working like a charm :) – Angela Pat. Apr 12 '16 at 21:01
  • @Sombriks how can i put your answer as the right solution? – Angela Pat. Apr 12 '16 at 21:03
  • @AngelaPat. feel free to compose an answer pointing what we debated here. make your answer as best as possible in order to help more people. – Sombriks Apr 13 '16 at 00:55

0 Answers0