我是业余做asp开发的,帮朋友做了一个程序,现在遇到一个问题,就是别人传了一些excel文件模板到网站服务器端,管理员登录后想一下通过复选按钮选 中多个文件,点击一次下载,就把多个选 中的文件一起打包下载到本地计算机上,请问这个用js 如何实现?能把代码晒一下就太感激了!

解决方案 »

  1.   

    在网上搜了一下,有人这样定的代码,但不感觉跟我需要的不是一回事,js 我不太懂的,帮忙看一下:
    function batchDownload(){
      try{ 
    var begin = document.getElementByIdx_x("begin").value;
    var end = document.getElementByIdx_x("end").value;
    begin = parseInt(begin);
    end = parseInt(end);
    //注意id一定要连贯
    for(i=begin;i<=end;i++){
    downloadFile(document.getElementByIdx_x("a_"+i).name);//调用下载方法
    }}catch(e){ } 
    }function downloadFile(url) 
    {   
    try{ 
    var elemIF = document_createElement_x("iframe");   
    elemIF.src = url;   
    elemIF.style.display = "none";   
    document.body.a(elemIF);   
    }catch(e){ } 
      

  2.   

    begin-end,是选择你需要下载其实位置和结束位置,然后创建一个iframe标签,设置src,设置url,然后调用document.body.a(elemIF);   大概是这样
      

  3.   

    js不能直接在前台操作的话,只能将多个文件路径传到后台,在后台打成包后,再执行下载操作。大家再帮看看这个代码,我感觉这个好像比上一个更具体易实现一些,不知道对不对,我怎样整合到我的asp中,希望各位赐教——————————
    当用户一次下载多个文件时,一般情况是,每下载一个文件,均要弹出一个下载的对话框,这给用户造成了很大不便。
    比较理想的情况是,用户选择多个文件后,服务器后端直接将多个文件打包为zip。下面贴出实现代码。前端JavaScript代码(使用javascript创建表单,通过提交表单的方式访问后端的MultiDownload):
    [html] view plain copy
    var tmpForm = document.createElement("form");  
    tmpForm.id = "form1" ;   
    tmpForm.name = "form1" ;                       
    document.body.appendChild(tmpForm);  
                                                      
    var tmpInput = document.createElement("input");   
    // 设置input相应参数   
    tmpInput.type = "text";   
    tmpInput.name = "fileUrls" ;   
    tmpInput.value = downloadUrls;  
    // 将该输入框插入到 tmpform 中  
    tmpForm.appendChild(tmpInput);  
                                      
    tmpForm.action= "servlet/MultiDownload" ;  
    tmpForm.method= "get";                       
    tmpForm.submit();  
    [html] view plain copy
    //从html从移除该form  
    document.body.removeChild(tmpForm);  后端MultiDownload代码:
    [html] view plain copy
    public class MultiDownload extends HttpServlet {  
        private static final long serialVersionUID = 1L;  
      
        /**  
         * @see HttpServlet#HttpServlet()  
         */  
        public MultiDownload() {  
            super();  
            // TODO Auto-generated constructor stub  
        }  
      
        /**  
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse  
         *      response)  
         */  
        protected void doGet(HttpServletRequest request,  
                HttpServletResponse response) throws ServletException, IOException {  
            // TODO Auto-generated method stub  
            String strArray = null;  
            strArray = request.getParameter("fileUrls");  
            //传递到servlet时,默认将编码转换为ISO-8859-1,将其重新转码为GBK  
            strArray=new String(strArray.getBytes("ISO-8859-1"), "GBK");   
            String[] filePathArray = strArray.split(",");  
            String zipFileName = "product.zip";  
            response.setContentType("application/x-msdownload" ); // 通知客户文件的MIME类型:  
            response.setHeader("Content-disposition" , "attachment;filename=" + zipFileName);  
            //要下载的文件目录  
            ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());  
            for (String filePath : filePathArray) {  
                File file = new File(filePath);  
                doZip(file, zos);  
            }             
            zos.close();  
        }  
        /**  
         *  打包为 zip 文件  
         * @param file 待打包的文件  
         * @param zos zip zip输出流  
         * @throws IOException   
         */  
        private void doZip(File file, ZipOutputStream zos)  
                throws IOException {  
            if(file.exists()) {  
                if (file.isFile()) {  
                    //如果是文件,写入到 zip 流中  
                    zos.putNextEntry(new ZipEntry(file.getName()));  
                    FileInputStream fis = new FileInputStream(file);  
                    byte[] buffer = new byte[1024];  
                    int r = 0;  
                    while ((r = fis.read(buffer)) != -1) {  
                        zos.write(buffer, 0, r);  
                    }  
                    zos.flush();                  
                    fis.close();  
                } else {  
                    //如果是目录。递归查找里面的文件  
                    String dirName = file.getName() + "/";  
                    zos.putNextEntry(new ZipEntry(dirName));  
                    File[] subs = file.listFiles();  
                    for (File f : subs) {  
                        makeZip(f, dirName, zos);  
                    }  
                }  
            }  
        }  
        /**  
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse  
         *      response)  
         */  
        protected void doPost(HttpServletRequest request,  
                HttpServletResponse response) throws ServletException, IOException {  
            // TODO Auto-generated method stub  
        }  
      
    }