简体   繁体   English

node.js将图像从http请求列表下载到服务器

[英]node.js download images to server from list of http requests

I'm very new at node so bear with me. 我在Node上很新,请耐心等待。 I'm trying to download a series of images from an external server. 我正在尝试从外部服务器下载一系列图像。 So far I have been able to get it working on a limited basis. 到目前为止,我已经能够使其在有限的基础上运行。 When I run the below code, only about half of the images make it though to the web page. 当我运行以下代码时,只有大约一半的图像进入了网页。 I know that I'm not doing this correctly, and am looking for some guidance. 我知道我没有正确执行此操作,并且正在寻找一些指导。 Here is the code that I have so far 这是我到目前为止的代码

var request = require("request"),
    fs = require("fs"),
    views = ['sitename1', 'sitename2', 'sitename3'...]
for (var view in views) {
    request({
        url: 'http://' + SERVERURL + '/api/2.2/sites/' + siteID + '/workbooks/' + views[view]['workbookID'] + '/views/' + views[view]['id'] + '/previewimage',
        headers: {
            'Content-Type': 'image/png',
            'X-Tableau-Auth': authToken
                }
            , encoding: 'binary'}).pipe(
                fs.createWriteStream('./public/images/thumbnails/' + SITE + '/views/' + views[view]['url'] + '.png'
            ))
        };

I want to point out that this does get some of the images saved correctly. 我想指出的是,这确实可以正确保存某些图像。 I believe what I'm missing is a callback to make sure that the file has been successfully saved before moving on to the next item in the list. 我相信我缺少的是一个回调,以确保在移至列表中的下一项之前,文件已成功保存。 I have no idea how to implement that. 我不知道如何实现。

Another quick note (not important) is that I'm trying to download images from a Tableau Server using the REST api. 另一个简短的说明(不重要)是,我正在尝试使用REST API从Tableau Server下载图像。

The face you're getting about half the images makes me wonder if you are using Tableau Online? 您所获得的面孔大约是一半的图像,这使我想知道您是否正在使用Tableau Online? If so, you need to ensure you're using the URI per the docs ( http://onlinehelp.tableau.com/current/api/rest_api/en-us/help.htm#REST/rest_api_concepts_fundamentals.htm#tableau-online-uris ). 如果是这样,则需要确保每个文档都使用URI( http://onlinehelp.tableau.com/current/api/rest_api/en-us/help.htm#REST/rest_api_concepts_fundamentals.htm#tableau-online -uris )。

Just figured it out using the async module 刚刚使用异步模块弄清楚了

async.eachSeries(views, (function(view, callback) {
            var thumbPath = 'public/images/thumbnails/' + req.session.SITE + '/views/' + req.session.views[view]['url'] + '.png'
                request({
                    url: 'http://' + SERVERURL + '/api/2.2/sites/' + req.session.siteID
                        + '/workbooks/' + req.session.views[view]['workbookID'] + '/views/' + req.session.views[view]['id'] + '/previewimage',
                    headers: {
                        'Content-Type': 'image/png',
                        'X-Tableau-Auth': req.session.authToken
                    }
                    }).pipe(
                        upload(thumbPath));
                    callback()
            }),
            function(err){
                if(err){
                    console.log("a thumb failed to download")
                } else {
                    console.log("all thumbs downloaded")
                }
            }
        )

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

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