根据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;
            }
        }
    }

你可能感兴趣的:(根据url下载多个文件)