我如何在同一功能中使用不同的uri

时间:2019-02-18 05:58:09

标签: javascript node.js

我正在尝试通过url进行下载功能

HTML中有3个按钮(下载,暂停,重新启动),分别调用download_file(),pause22()和restart_test()。

它们使用在download_file()中声明的包含请求对象的相同变量[req]。

如果我只打一次就可以了。

但是问题是我需要制作下载列表。因此每个download_file()需要使用不同的URL,并且用户会同时多次调用它。同样,当请求结束时,需要在单击的按钮上显示一条消息。

我可以重用功能吗?

如果我通过url来实现每个不同的功能,那将是愚蠢的。

如何区分点击的内容?

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>

2 个答案:

答案 0 :(得分:1)

我认为您应该将req保存到对象中。

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>

答案 1 :(得分:1)

您可以传递一个链接/文件名数组,然后使用for循环遍历它们,并发出如下这样的单个请求:

// 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;
    })
  };
}