简体   繁体   English

使用fs和http模块使用Node.js下载zip文件不起作用

[英]Downloading a zip file with Node.js using fs and http module not working

Im using the following code to download a zip file. 我正在使用以下代码下载zip文件。 If you put the URL into a browser you will see that it starts downloading. 如果将URL放入浏览器,您将看到它开始下载。 But when I use Node to download the zip, the output test.zip file contains this: 但是,当我使用Node下载zip时,输出的test.zip文件包含以下内容:

<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="http://patentscdn.reedtech.com/ipg150804-zip/ipg150804.zip?sv=2014-02-14&amp;sr=b&amp;sig=u1W37AFnH9DAcqc7k4tDABaV14eSlchN8fJJ1%2FOkWEo%3D&amp;st=2015-08-04T04%3A00%3A00Z&amp;se=9999-12-31T05%3A00%3A00Z&amp;sp=r">here</a></body>

Heres the Node.js code: 这是Node.js代码:

var http = require('http');
var fs = require('fs');
var url = 'http://patents.reedtech.com/downloads/GrantRedBookText/2015/ipg150804.zip';

http.get(url, function(response) {
    response.on('data', function(data) {
        fs.appendFileSync('test.zip', data);
    });
    response.on('end', function() {
        console.log('hello');
    });
});

There is an HTTP redirect, you can do the following 有一个HTTP重定向,您可以执行以下操作

var http = require('follow-redirects').http;

this will enable http to follow redirects. 这将使http能够跟随重定向。 I suggest you use request library. 我建议您使用请求库。

Check How do you follow an HTTP Redirect in Node.js? 检查如何在Node.js中遵循HTTP重定向? for more details 更多细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM