我是业余做asp开发的,帮朋友做了一个程序,现在遇到一个问题,就是别人传了一些excel文件模板到网站服务器端,管理员登录后想一下通过复选按钮选 中多个文件,点击一次下载,就把多个选 中的文件一起打包下载到本地计算机上,请问这个用js 如何实现?能把代码晒一下就太感激了!
解决方案 »
- 【新手求助】document.forms[0].inf is undefined
- lhgdialog穿越frame问题
- 跪求“加入收藏夹”代码!!要能用的啊!!
- jquery $.getjson()
- 菜单项的奇怪问题,急等
- 求一检索JavaScript代码,功能跟windows的ctrl+f 一样就行(UP者有分)
- 请问在有没有firefox中操作XML的 js代码.
- jsp、javascrip写excel模板,进行b/s结构下套打的问题?望高手赐教
- 急,如何给图片加上链接,高手请进!
- 如何用Javascript在網頁上畫一條斜線或一個圓
- DOM编程艺术综合示例-增强表单中的问题
- require js引入jquery插件并使用,报错
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){ }
}
当用户一次下载多个文件时,一般情况是,每下载一个文件,均要弹出一个下载的对话框,这给用户造成了很大不便。
比较理想的情况是,用户选择多个文件后,服务器后端直接将多个文件打包为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
}
}