简体   繁体   English

如何使用 JavaScript 从浏览器控制台批量下载文件?

[英]How to download files in bulk from browser console using JavaScript?

I have no experience with web programming, so my question would be a very simple one.我没有网络编程经验,所以我的问题很简单。 I want to download a lot of files by filling out forms in a web page.我想通过在网页中填写表格来下载大量文件。 The web page's extension is .aspx , and I am interested in only one field and a button.网页的扩展名是.aspx ,我只对一个字段和一个按钮感兴趣。 By fooling around with the console in my browser, I figured out that executing:通过在浏览器中玩弄控制台,我发现执行:

document.getElementById('TxtRegNo').value = 'blahblah`;

will fill the concerned field.将填写相关字段。 Also doing a也在做一个

__doPostBack("ImageButton1","Click");

will download the .pdf file curresponding to blahblah .将下载对应blahblah.pdf文件。 The actual value which needs to be entered is a sequence like PAG-1200 to PAG-1900.需要输入的实际值是一个类似 PAG-1200 到 PAG-1900 的序列。 I tried using a for loop, like this:我尝试使用 for 循环,如下所示:

for (var i = 21618; i < 21621; i++) 
{
document.getElementById('TxtRegNo').value = 'B14-' + i;
__doPostBack("ImageButton1","Click");
}

but it doesnot work as expected.但它没有按预期工作。 only the last document gets downloaded, and I get this in the console:只下载了最后一个文档,我在控制台中得到了这个:

在此处输入图片说明

Thought this error does not come whe nI run in FireFox's console, I can still run only one file.我以为在 FireFox 的控制台中运行时不会出现此错误,但我仍然只能运行一个文件。 Could anyone tell me how to do this?谁能告诉我如何做到这一点?

Try this:试试这个:

  1. Inject jQuery to the page via the console, as explained here: Include jQuery in the JavaScript Console通过控制台将 jQuery 注入页面,如下所述: 在 JavaScript 控制台中包含 jQuery
  2. In your for loop clone the form, set the values as you wish and submit the form jQuery('form').clone().find('#TxtRegNo').val('blah').parent('form').submit();在您的 for 循环中克隆表单,根据需要设置值并提交表单jQuery('form').clone().find('#TxtRegNo').val('blah').parent('form').submit();

If the page contains more than one form, you should specify it.如果页面包含多个表单,您应该指定它。 'form' works like css selectors here. 'form'在这里就像 css 选择器一样工作。 This will find all forms on the page.这将找到页面上的所有表单。 Just use '#elementId' or '.elementsClassName' to be more concrete, if necessary.如有必要,只需使用'#elementId''.elementsClassName'更具体。

Maybe you also need to change the name of the form (to be able to submit the forms simulatiously).也许您还需要更改表单的名称(以便能够以模拟方式提交表单)。 I didn't try it, this is just a guess.我没试过,这只是猜测。

If you want to split the code to several lines you can also do this:如果要将代码拆分为多行,也可以这样做:

var myFormClone = jQuery('form').clone();
myFormClone.find('#TxtRegNo').val('blah');
myFormClone.attr('name', 'uniquename_' + iterationVariableOfForLoop);
myFormClone.submit();

If the submit failes, try:如果提交失败,请尝试:

myFormClone.get(0).submit();

Good luck!祝你好运!

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

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