简体   繁体   中英

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

I'm very new at node so bear with me. 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.

The face you're getting about half the images makes me wonder if you are using 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 ).

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")
                }
            }
        )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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