根据url下载多个文件

第一想法是,for循环里使用window.location.href,因为之前有下载单个,但是问题是它只下载第一个,后面的就不下载了,debugger调试倒是可以下载多个,然后就百度最后发现可以使用两种 iframe和es6的promise,后面那个不知道怎么用以后再说。。。附上代码

for(let i=0; i< arr.length; i++) {
                var url = "${Sys_Url}/recordvideo/exportVideo.do?RecordVideoId=" + arr[i];
                window.location.href=url;
            }
@RequestMapping(value = "/exportVideo")
    @ResponseBody
    public String exportVideo(HttpServletRequest request, HttpServletResponse response) throws Exception {
        
        String recordVideoId = getParamters("RecordVideoId");
        VdRecordvideo video = iVdRecordvideoService.get(recordVideoId);
        

        if (!StringPlus.isNullOrEmpty(video)) {
            PmAttach attach = iPmAttachService.get(video.getAttachId());
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            // 截取字符串
            String[] list = video.getVideoPath().split(Pattern.quote("/"));
            response.setHeader("Content-Disposition", "attachment;fileName=" + list[5]);
            //SysConfig.get("Sys_Url")
            String path =SysConfig.get("Sys_Url") +  "/" + attach.getAttachPath();
            
            InputStream in = null;
            OutputStream out = null;
            // 获取数据
            try {
                // 建立url请求对象
                URL url = new URL(path);
                // 建立url连接
                URLConnection connection = url.openConnection();
                in = connection.getInputStream();
                out = response.getOutputStream();
                byte[] b = new byte[2048];
                int count = 0;
                while((count = in.read(b))>0) {
                    out.write(b,0,count);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO: handle exception
                e.printStackTrace();
            }finally {
                in.close();
                out.close();
            }
        }
        return null;
    }
  //批量导出视频
    function exportVideos() {
        var key = $.getVal('dgCheck');
        var arr = key.split(',');
        
        //var url = "${Sys_Url}/recordvideo/exportVideos.do?key=" + key;
        if (key == '' || key == undefined) {
            $.alert('请选择一项导出');
            return;
        } else {
            for(let i=0; i< arr.length; i++) {
                 var url = "${Sys_Url}/recordvideo/exportVideo.do?RecordVideoId=" + arr[i];
                 const iframe = document.createElement("iframe");
                 iframe.style.display = "none"; // 防止影响页面
                 iframe.style.height = 0; // 防止影响页面
                 iframe.src = url; 
                 document.body.appendChild(iframe); // 这一行必须,iframe挂在到dom树上才会发请求
                 // 5分钟之后删除(onload方法对于下载链接不起作用,就先抠脚一下吧)
                 setTimeout(function(){
                   iframe.remove();
                 }, 5 * 60 * 1000);
                //window.location.href=url;
            }
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 人丑就该多看书 文章来自其他博文。 *.location.href 用法: javascript> functio...
    樱花树下_529d阅读 389评论 0 0
  • 五十三:请解释 JavaScript 中 this 是如何工作的。1.方法调用模式当一个函数被保存为一个对象的属性...
    Arno_z阅读 595评论 0 2
  • 你不知道JS:异步 第三章:Promises 接上篇3-1 错误处理(Error Handling) 在异步编程中...
    purple_force阅读 1,416评论 0 2
  • 面试题一:https://github.com/jimuyouyou/node-interview-questio...
    R_X阅读 1,638评论 0 5
  • 文/岑岚 文章目录:如果时光不记得——目录 这世上总有一人踏着时光来寻你——目录 前情回顾:如果时光不记得(十三)...
    岑岚阅读 1,031评论 22 12