如何从javascript下载多个文件 [英] How to Download multiple files from javascript

查看:168
本文介绍了如何从javascript下载多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试循环使用window.location.href下载多个文件

我有一个表,可以在其中选择文件,然后运行选定的循环并 尝试导航到文件路径以下载文件.

I have a table in which i can select file's, then i run a loop of selected and try navigate to the file path to download the files.

我一直只下载最新的文件.

I keep only getting the last file to download.

我认为这是由于位置herf仅在我的javascript完成后才执行操作,而不是在代码运行时执行.

I think it's due to the location herf only taking action after my javascript finishes and not as the code runs.

当我在window.location.herf上有一个断点时,它仍然仅下载最后一个文件,并且仅当我让代码运行通过时.

When i have a break point on the window.location.herf it still only downloads the last file and only when i let the code run through.

是否有更好的方法可以从javascript循环启动多次下载.

Is there a better way to initiate multiple downloads from a javascript loop.

$("#btnDownload").click(function () {
  var table = $('#DocuTable').DataTable();
  var rows_selected = table.rows('.selected').data();
  $.each(rows_selected, function (i, v) {
    window.location.href = v.FilePath;
  });
});

推荐答案

我将使用iframe和脚本来强制将文件下载为 Joe Enos cmizzi 有建议.

I would make use of iframes and a script to force the download of the files as Joe Enos and cmizzi have suggested.

这里的答案将有助于JavaScript为每个文件打开多个iframe: 一次下载多个文件

The answer here will help with JavaScript for opening multiple iframes for each file: Download multiple files with a single action

如果URL实际上是可以在网络上正确提供的内容,则流行语言的答案将有助于强制下载:

The answers for popular languages will help with forcing downloads if the URL is actually something that can be served correctly over the web:

  • PHP: How to force file download with PHP
  • .Net: Force download of a file on web server - ASP .NET C#
  • NodeJS: Download a file from NodeJS Server using Express
  • Ruby: Force browser to download file instead of opening it

确保将链接更改为指向您的下载脚本,并确保添加了适当的安全检查.您不想允许任何人滥用您的脚本.

Ensure you change the links to point to your download script and also make sure you add the appropriate security checks. You wouldn't want to allow anyone to abuse your script.

这篇关于如何从javascript下载多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆