小编alo*_*uan的帖子

NodeJS - 如何通过请求下载文件?

我有一个ExternalServe(在Localhost上运行)当我使用Browser请求时:

本地主机:2013/ExternalServer/getfilebyname文件名= getStatus.json

然后浏览器将getStatus.json下载到Download Folder.

在我的NodeJS项目中,我想下载getStatus.json文件,我做了:

download.js

var http = require('http');
var fs = require('fs');

function getFile (){

  var file = fs.createWriteStream("./../lib/user.json");
  var req = http.get("http://localhost:2013/ExternalServer/getfilebyname?filename=getStatus.json", function(res) {
    res.pipe(file);
});
}

getFile();
Run Code Online (Sandbox Code Playgroud)

但是当我运行:node download.js系统返回

<html><head><title>Apache Tomcat/8.0.0-RC1 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 401 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>This request requires HTTP authentication.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/8.0.0-RC1</h3></body></html>
Run Code Online (Sandbox Code Playgroud)

怎么解决?

最良好的问候

javascript json download request node.js

6
推荐指数
1
解决办法
2万
查看次数

使用Loop下载许多文件时,Nodejs丢失了数据

今天,我尝试从我的服务器下载许多文件

download.js

function getPhotos(req, res) {
  //Get User Photos
  var fileReader = fs.readFile('./../data/user.json', 'utf8', function(err, data) {
    if (err)
      console.log(err);
    else {
      var dataJson = JSON.parse(data);
      //console.log(dataJson.Person);
      for (var i = 0; i < dataJson.Person.length; i++) {
        var options = {
          host : '10.16.47.128', // Local Server IPAddress
          port : 2013, //Port
          path : '/ExternalServer/avatar/' + dataJson.Person[i].Username + '.jpg',
        };
        //console.log(dataJson.Person[i].Username);
        var fileAvatarPhotos = fs.createWriteStream('./../avatar/' + dataJson.Person[i].Username + '.jpg');
        fs.exists(fileAvatarPhotos, function(exists) {//Check Exist File
          if (exists) {
            var req = …
Run Code Online (Sandbox Code Playgroud)

javascript download node.js

2
推荐指数
1
解决办法
2251
查看次数

标签 统计

download ×2

javascript ×2

node.js ×2

json ×1

request ×1