web上传文件的三种解决方案分享给大家:
这里我要使用的是form法。通过为表单元素设置enctype=”multipart/form-data”属性,让表单提交的数据以二进制编码的方式提交,在接收此请求的servlet中用二进制流来获取内容,就可以取得上传文件的内容,从而实现文件的上传。
表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值:
在网上找到了两种方式,php配合apc实现和利用uploadprogress实现,这次我要使用的是uploadprogress,点击地址可以下载到php相应版本的dll。安装php_uploadprogress.dll扩展,重启apache。
进度条实现原理:
这里用到了一个iframe无刷新上传文件的方法。
上传完成后的样子如图:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<body>
<div style="padding:20px">
<form action="action.php" enctype="multipart/form-data" method="post" target="iframeupload">
<iframe name="iframeupload" width="400" height="400" frameborder='1'></iframe>
<input type="hidden" name="upload_identifier" value="1" />
<input id="file1" name="file1" type="file"/>
<input value="上传" type="submit" onclick="startprogress()"/>
</form>
</div>
<div style="width: 500px; height: 20px;border:1px solid red">
<div style="position: relative; height: 20px; background-color: purple; width: 0%;" class="barinner"></div>
</div>
<div id='shownum'></div>
<div class="prbar">
<div class="prpos barinner"></div>
</div>
</body>
|
上面的html代码中要注意下upload_identifier,这个是用来定位查看哪个文件的上传进度的。我这里就写死为一个1,大家可以写成一个php生成的随机数。下面是js脚本:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
var pronum=0;
var loop=0;
var progressresult = "";
var interval;
function sendurl() {
$.ajax({
type : 'get',
url : "getprogress.php",
async : true,
cache : false,
datatype : 'json',
data: "progress_key=" + $('input[name=upload_identifier]').val(),
success : function(e) {
pronum=parseint(e);
if(e){
$('.barinner').css('width', pronum+"%");
$('#shownum').html(pronum+"%");
settimeout("getprogress()", 200);
}else{
if(interval == 1){
$('.barinner').css('width', "100%");
$('#shownum').html("100%");
}
}
}
});
}
function getprogress(){
loop++;
sendurl();
}
function startprogress(){
interval = 1;
$('.barinner').css('width', pronum+"%");
$('#shownum').html(pronum+"%");
settimeout("getprogress()", 500);
}
|
下面是getprogress.php文件中的内容:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
if (function_exists("uploadprogress_get_info")) {
$info = uploadprogress_get_info($_get['progress_key']);
if(!empty($info)){
if(($info['bytes_uploaded'] < $info['bytes_total']) && !empty($info['bytes_uploaded']) && !empty($info['bytes_total'])){
$pronum = floor(($info['bytes_uploaded']/$info['bytes_total'])*100);
}else{
$pronum = 100;
}
echo $pronum;
}else{
echo 0;
}
}
|
在上传完成后,我展示了两种进度条的css,第二种是用最新的css3写的。用到了一些css3的动画属性。
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
.prbar {
margin:5px;
width:500px;
background-color:#dddddd;
overflow:hidden;
/* 边框效果 */
border: 1px solid #bbbbbb;
-moz-border-radius: 15px;
border-radius: 15px;
/* 为进度条增加阴影效果 */
-webkit-box-shadow: 0px 2px 4px #555555;
-moz-box-shadow: 0px 2px 4px #555555;
box-shadow: 0px 2px 4px #555555;
}
/* no rounded corners for opera, because the overflow:hidden dont work with rounded corners */
doesnotexist:-o-prefocus, .prbar {
border-radius:0px;
}
.prpos {
width:0%;
height:30px;
background-color:#3399ff;
border-right:1px solid #bbbbbb;
/* css3 进度条渐变 */
transition: width 2s ease;
-webkit-transition: width 0s ease;
-o-transition: width 0s ease;
-moz-transition: width 0s ease;
-ms-transition: width 0s ease;
/* css3 stripes */
background-image: linear-gradient(135deg,#3399ff 25%,#99ccff 25%,#99ccff 50%, #3399ff 50%, #3399ff 75%,#99ccff 75%,#99ccff 100%);
background-image: -moz-linear-gradient(135deg,#3399ff 25%,#99ccff 25%,#99ccff 50%, #3399ff 50%, #3399ff 75%,#99ccff 75%,#99ccff 100%);
background-image: -ms-linear-gradient(135deg,#3399ff 25%,#99ccff 25%,#99ccff 50%, #3399ff 50%, #3399ff 75%,#99ccff 75%,#99ccff 100%);
background-image: -o-linear-gradient(135deg,#3399ff 25%,#99ccff 25%,#99ccff 50%, #3399ff 50%, #3399ff 75%,#99ccff 75%,#99ccff 100%);
background-image: -webkit-gradient(linear, 100% 100%, 0 0,color-stop(.25, #99ccff), color-stop(.25, #3399ff),color-stop(.5, #3399ff),color-stop(.5, #99ccff),color-stop(.75, #99ccff),color-stop(.75, #3399ff),color-stop(1, #3399ff));
background-image: -webkit-linear-gradient(135deg,#3399ff 25%,#99ccff 25%,#99ccff 50%, #3399ff 50%, #3399ff 75%,#99ccff 75%,#99ccff 100%);
background-size: 40px 40px;
/* background stripes animation */
animation: bganim 3s linear 2s infinite;
-moz-animation: bganim 3s linear 2s infinite;
-webkit-animation: bganim 3s linear 2s infinite;
-o-animation: bganim 3s linear 2s infinite;
-ms-animation: bganim 3s linear 2s infinite;
}
@keyframes bganim {
from {background-position:0px;} to { background-position:40px;}
}
@-moz-keyframes bganim {
from {background-position:0px;} to { background-position:40px;}
}
@-webkit-keyframes bganim {
from {background-position:0px;} to { background-position:40px;}
}
@-o-keyframes bganim {
from {background-position:0px;} to { background-position:40px;}
}
@-ms-keyframes bganim {
from {background-position:0px;} to { background-position:40px;}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关文章
猜你喜欢
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 91
-
2025-05-25 37
-
2025-05-25 84
-
2025-05-25 64
-
2025-05-29 43
热门评论







