But we don't check the result return cb err. Buzut Buzut 4, 4 4 gold badges 41 41 silver badges 50 50 bronze badges. The request module just works straight for HTTPs. S Ventura. It's more error prone without a doubt. Anyway, in any case where using request module is an option, I'd advise it as it's way higher level and thus, easier and efficient.
Alex, nope, this is an error message and there's a return. So if response. Thank you for showing example using request module. Show 3 more comments. MikeL 5, 38 38 silver badges 41 41 bronze badges. Augusto Roman Augusto Roman 6 6 silver badges 4 4 bronze badges. What for are you storing request into a variable? You are right, there is no need to save the request, its not used anyway.
That's what you mean? Yes, I think that's what polkovnikov. Per default the fs. Bjarke Pjedsted Bjarke Pjedsted 4 4 silver badges 8 8 bronze badges. Two comments on this: 1 it should probably reject Error objects, not strings, 2 fs. This works great!
No errors popped up but the. Removing the logic for the flag fixed it. Just wanted to point that out if someone had the issues with the approach. Is there a memory leak in this answer: stackoverflow. You can add timeout like I did in http. The memory leak is only if the file take too long to be downloaded. Based on the other answers above and some subtle issues, here is my attempt. Check the file does not exist before hitting the network by using fs.
Only create the fs. This reduces the amount of fs. Even on a OK we can still possibly reject due to an EEXIST file already exists imagine another process created the file whilst we were doing network calls. Recursively call download if you get a Moved Permanently or Found Moved Temporarily redirect following the link location provided in the header.
The issue with some of the other answers recursively calling download was that they called resolve download instead of download This way the nested chain of promises resolve in the correct order. PORT app. Arman Septian Arman Septian 25 3 3 bronze badges.
Khoa Tran dang Khoa Tran dang 20 2 2 bronze badges. How is this an answer? You have not explained anything that is different from what the OP posted. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.
The Overflow Blog. Podcast An oral history of Stack Overflow — told by its founding team. Millinery on the Stack: Join us for Winter Summer? Improve this question. LeeTee LeeTee 6, 14 14 gold badges 75 75 silver badges bronze badges. Where is the directory you are trying to download? Can you access the list of files, the list of filepaths to download?
Type your folder path in your browser and parse this html file. I didn't downvote, but it is likely because the question is a bit hard to understand, and it doesn't seem easy to track down where the issue might be coming from.
Show 3 more comments. Active Oldest Votes. Improve this answer. Hi, thanks for your help. Any chance you could elaborate on how to do this exactly? I have just installed express within my existing node webkit app and added the 4 lines of code you have displayed above.
I needed to download a file from behind a login, which was being handled by Puppeteer. In the end I downloaded with request , after copying the cookies over from the Puppeteer instance. One way I found was using addScriptTag method.
Works in both headless either False or True. Using this any kind of webpage can be downloaded. I had a more difficult variation of this, using Puppeteer Sharp. I needed both Headers and Cookies set before the download would start.
In essence, before the button click, I had to process multiple responses and handle a single response with the download. Once I had that particular response, I had to attach headers and cookies for the remote server to send the downloadable data in the response. If your needs to download a file are more simplistic, you can probably use the other methods mentioned on this thread, or the linked thread. I needed to log into a website, and download some.
Headed was fine, headless failed no matter what I tried. Looking at the Network errors, the download is aborted, but I couldn't quickly determine why. So, I intercepted the requests and used node-fetch to make the request outside of puppeteer. This required copying the fetch options, body, headers and adding in the access cookie. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. What if the data was coming in from a HTTP request instead of a file and we had to let users download the file in a streaming way?
For static files like pdfs, Word docs, etc. That works well for assets although a dedicated serving proxy like nginx is recommended. But for anything that requires secured access, the accepted method is better. Generally speaking for docs and files containing information, I wouldn't recommend using the public method.
Of course, that means you'll need to be able to figure out a user's access level just based on the url of the secure document, or whatever. Vedran Vedran 1, 1 1 gold badge 15 15 silver badges 34 34 bronze badges. In Express 4. Benoit Blanchon Benoit Blanchon There are several ways to do it This is the better way res. Let's say you have created a form for files to be uploaded by the User. Digvijay Yamagekar Digvijay Yamagekar 11 1 1 bronze badge. The Overflow Blog.
0コメント