文件上传进度条-----servlet+js

闲的没事,突然想起读书的时候文件上传进度条没有实现,现在想想,那时候真是太聪明了。如今,就刚刚,蛋疼得我做了一个,正如我所想到的一样,同事们确实是一阵嘲笑,还有一个小伙子居然说:‘这种东西网上很多成熟的东西了,我们不应该拘泥于这种小技术、、、’;我晕倒了,哎,学校里没实现的东西,虽然现在看来信手拈来,但是,自己做的东西总比网上荡好,我们很多同事就是这样,网上荡。。。对此,我只能说无语。。。。


其实上传进度条很简单,最简单的就是我下面的了,当然复杂的也有,不过我没用过。我以前也是用flash的,
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传进度条实例</title>
<link style="text/css" rel="stylesheet" href="css/index.css">
<script type="text/javascript" src="script/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="script/upload.js"></script>
</head>
<body>

<fieldset>
<legend>文件上传实例</legend>
<form id="form" action="file.upload" method="post" enctype="multipart/form-data" target="frm">
请选择上传的文件:<input type="file" name="uploadFile" />
</form>
<button id="btn">上传</button>
</fieldset>

<fieldset>
<legend>进度显示</legend>
<div id="process"><div id="data"></div></div>
</fieldset>
<iframe id="frm" name="frm" style="display:none;"></iframe>
</body>
</html>



package servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.DefaultFileItemFactory;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


@SuppressWarnings("deprecation")
public class UpLoad extends HttpServlet {
private static final long serialVersionUID = 1L;

@SuppressWarnings("rawtypes")
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DefaultFileItemFactory factory = new DefaultFileItemFactory();
HttpSession session = request.getSession();
ServletFileUpload upload = new ServletFileUpload(factory);
try{
List tempList = upload.parseRequest(request);
Iterator it = tempList.iterator();
while(it.hasNext()){
FileItem item = (FileItem)it.next();
if(item.isFormField()){
/**处理常规文本域**/
}else{
if(item.getName()!=null&&!item.getName().equals("")){
Long size = item.getSize();
System.out.println(size);
File tempFile = new File("F:/"+item.getName());
byte[] b = new byte[100];

InputStream in = item.getInputStream();
OutputStream out =new FileOutputStream(tempFile);
int len = 0;
int current = 0;
session.setAttribute("total", size);
while((len=in.read(b))>0){
current += len;

Thread.sleep(10);//故意延迟,以便看出效果
session.setAttribute("current", current);
out.write(b,0,len);
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}

}




var total = 0;//文件总大小
var current = 0;//当前大小
var present= 0;//百分比
var timer = null;//定时触发事件
(function($){
$(function(){
$('#btn').click(function(){
timer = window.setInterval('getData()', 500);
$('#btn').attr('disabled','disabled');
$('#form').submit();
});
});
})(jQuery);
var getData = function(){
$.ajax({
url:'file.data',
success:function(msg){
msg = eval('(' + msg + ')');
total = msg.total;
current = msg.current;
if(present==100){
$('#btn').attr('disabled','');
window.clearInterval(timer);
}
present= (current/total)*100;
$('#data').attr('innerHTML',present.toFixed(2)+'%');
}
});
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-upload 是 Element UI 提供的文件上传组件,支持多文件上传。如果需要为 el-upload 组件添加多文件上传进度条,可以使用 el-progress 组件结合 el-upload 的 events 属性实现。 首先,在 el-upload 组件中添加 `show-file-list="false"` 属性,隐藏上传文件列表。然后,在 el-upload 组件中添加一个 el-progress 组件,将 el-progress 组件的 `percentage` 属性绑定到一个变量(如 `uploadPercent`),并设置 el-progress 组件的 `stroke-width` 属性和 `status` 属性。最后,在 el-upload 组件的 `before-upload` 和 `on-progress` 事件中更新 `uploadPercent` 变量的值。 以下是示例代码: ```html <el-upload class="upload-demo" action="/upload" :on-success="handleSuccess" :on-error="handleError" :before-upload="beforeUpload" :on-progress="onProgress" :show-file-list="false"> <el-button size="small" type="primary">点击上传</el-button> <el-progress :percentage="uploadPercent" :stroke-width="8" status="success"></el-progress> </el-upload> ``` ```javascript data() { return { uploadPercent: 0 } }, methods: { beforeUpload(file) { // 在上传前重置上传进度条 this.uploadPercent = 0 }, onProgress(event, file, fileList) { // 更新上传进度条的值 this.uploadPercent = event.percent }, handleSuccess(response, file, fileList) { // 上传成功后重置上传进度条 this.uploadPercent = 0 }, handleError(error, file, fileList) { // 上传失败后重置上传进度条 this.uploadPercent = 0 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值