g.developer
g.developer

Reputation: 165

how can I use different uri in same function

I'm trying to make a download function by url

In HTML there are 3 buttons (download, pause, restart) calls download_file(),pause22(), restart_test() .

They use same variable [req] declared in download_file() which contains a request object.

It works ok If I call only one time.

but The problem is I need to make download lists. so each download_file() needs to use different URL and it would be called at the same time many times by users. also when request is ended need to show a message at the clicked button.

can I reuse functions?

It would be stupid if I make each different functions by url.

how could I distinguish what is clicked?

js

function download_file(event, fileURL) {
  req = request({
    method: 'GET',
    uri: fileURL,
  });
  var out = fs.createWriteStream(finalPath);
  req.pipe(out); 
  req.on('data', function(chunk) {
    received_bytes += chunk.length;
  });
  req.on('end', function() {
    subW = test.substring(9, 18).trim();
    $("." + subW).text('finished');
    endV = 2;
  });
}

//resume function
function restart_test() {
  req = request({
    method: 'GET',
    uri: fileURL
  });
  var out = fs.createWriteStream(finalPath);
  req.pipe(out); 
  req.on('data', function(chunk) {
    received_bytes += chunk.length;
  });
  req.on('end', function() {
    subW = test.substring(9, 18).trim();
    $("." + subW).text('finished');
    endV = 2;
  });
}
//pause
$('#btn-pause').click(function(e) {
  console.log('pause function called');
  req.pause();
});

html

<tr>
        <td class="test"><a class="checkBtn checkBtn2" onclick="download_file(event, '100mb.bin')">download</a></td>
        <td><a class="pauseBtn pauseBtn2" id="btn-pause" value="ACTION">pause</a><a class="resumeBtn resumeBtn2" onclick="restart_test();" value="ACTION">restart</a></td>
      </tr>
      <tr>
        <td class="test"><a class="checkBtn checkBtn2" onclick="download_file(event, '1000mb.bin')">download</a></td>
        <td><a class="pauseBtn pauseBtn2" id="btn-pause" value="ACTION">pause</a><a class="resumeBtn resumeBtn2" onclick="restart_test();" value="ACTION">restart</a></td>
      </tr>

Upvotes: 0

Views: 62

Answers (2)

Dabees
Dabees

Reputation: 504

You can pass an array of links/filename and then use for loop to iterate over them and make individual requests like this for example:

// array of filenames/url
fileNameArray = ['100.txt', '432.txt', '354.bin']

function download_file(event, fileNameArray) {
  for (link in fileNameArray){
    req = request({
      method: 'GET',
      uri: link,
    });
    var out = fs.createWriteStream(finalPath);
    req.pipe(out); 
    req.on('data', function(chunk) {
      received_bytes += chunk.length;
    });
    req.on('end', function() {
      subW = test.substring(9, 18).trim();
      $("." + subW).text('finished');
      endV = 2;
    })
  };
}

Upvotes: 1

I think you should save req into object.

JS

var listReqs = {};

function download_file(event, filename) {
  var req = request({
    method: 'GET',
    uri: fileURL,
  });
  var out = fs.createWriteStream(finalPath);
  req.pipe(out); 
  req.on('data', function(chunk) {
    received_bytes += chunk.length;
  });
  req.on('end', function() {
    subW = test.substring(9, 18).trim();
    $("." + subW).text('finished');
    endV = 2;
  });
  listReqs[filename] = req;
}

//resume function
function restart_test(filename) {
  var req = listReqs[filename];
  var out = fs.createWriteStream(final path);
  req.pipe(out); 
  req.on('data', function(chunk) {
    received_bytes += chunk.length;
  });
  req.on('end', function() {
    subW = test.substring(9, 18).trim();
    $("." + subW).text('finished');
    endV = 2;
  });
}
//pause
$('.pauseBtn2').click(function(e) {
  console.log('pause function called');
  var filename = $(this).attr('filename');
  listReqs[filename].pause();
});

Html

<tr>
        <td class="test"><a class="checkBtn checkBtn2" onclick="download_file(event, '100mb.bin')">download</a></td>
        <td><a class="pauseBtn pauseBtn2" filename="100mb.bin" value="ACTION">pause</a><a class="resumeBtn resumeBtn2" onclick="restart_test('100mb.bin');" value="ACTION">restart</a></td>
      </tr>
      <tr>
        <td class="test"><a class="checkBtn checkBtn2" onclick="download_file(event, '1000mb.bin')">download</a></td>
        <td><a class="pauseBtn pauseBtn2" filename="1000mb.bin" value="ACTION">pause</a><a class="resumeBtn resumeBtn2" onclick="restart_test('1000mb.bin');" value="ACTION">restart</a></td>
      </tr>

Upvotes: 1

Related Questions