ε=(?д?`*)??… 簡(jiǎn)單而粗暴的無(wú)刷新上傳圖片敌呈,前端效果如下面:
親測(cè)可以上傳圖片:
(^-^)那就不多廢話(huà)啦,直接上代碼
首先在站點(diǎn)目錄下創(chuàng)建以下文件夾 為了方便復(fù)制代碼 樣式腳本都置于index.html里面了?(?`н′?)?:
index.html:
<!DOCTYPE html>
<html>
<head>
<title>楸木的小貓不吃漁</title>
<link rel="stylesheet" type="text/css" href="./accets/css/style.css">
<style>
.Upload_picture{
position: fixed;
left: 0;
right: 0;
display: none;
top: 0;
bottom: 0;
background: rgba(0, 0, 0, .3);
z-index: 99999;
}
.Upload_pictureaduto{
width: 800px;
height: 500px;
background: #fff;
margin: 200px auto;
}
.Upload_picturetitle{
height: 40px;
padding: 0px 10px;
border-bottom: 1px solid #ccc;
line-height: 40px;
}
.Upload_picturetitle h1{
font-size: 18px;
float: left;
color: #fb5609;
}
.Upload_picturetitle span{
float: right;
font-size: 18px;
color: #fb5609;
}
.addbtn{position: relative;overflow: hidden;margin-right: 4px;display:inline-block;
*display:inline;padding:4px 10px 4px;font-size:14px;line-height:18px;
*line-height:20px;color:#fff;
text-align:center;vertical-align:middle;cursor:pointer;background:#5bb75b;
border:1px solid #cccccc;border-color:#e6e6e6 #e6e6e6 #bfbfbf;
border-bottom-color:#b3b3b3;-webkit-border-radius:4px;
-moz-border-radius:4px;border-radius:4px;}
.addbtn input{position: absolute;top: 0; right: 0;margin: 0;border:solid transparent;
opacity: 0;filter:alpha(opacity=0); cursor: pointer;}
.progress{position:relative; margin-left:200px; margin-top:-24px;
width:200px;padding: 1px; border-radius:3px; display:none}
.bar {background-color: green; display:block; width:0%; height:20px;
border-radius:3px; }
.percent{position:absolute; height:20px; display:inline-block;
top:3px; left:2%; color:#fff }
.files{height:22px; line-height:22px; margin:10px 0}
.delimg{margin-left:20px; color:#090; cursor:pointer}
.Alcohol2 a img,#showimg img{
width: 191px;
height: 188px;
}
.Keyshoppingcart{
cursor: pointer;
margin-right: 20px;
float: left;
/* width: 100px; */
background: #ef7d48;
height: 40px;
line-height: 40px;
/* margin-bottom: 20px; */
color: #fff;
-moz-border-radius: 10px;
-webkit-border-radius: 4px;
border-radius: 4px;
margin-top: 20px;
padding: 0px 10px;
font-size: 18px;
}
.updiy{
cursor: pointer;
margin-right: 20px;
float: left;
/* width: 100px; */
background: #ef7d48;
height: 40px;
line-height: 40px;
/* margin-bottom: 20px; */
color: #fff;
-moz-border-radius: 10px;
-webkit-border-radius: 4px;
border-radius: 4px;
margin-top: 20px;
padding: 0px 10px;
font-size: 18px;
}
</style>
</head>
<body>
<div class="Alcohol2">
點(diǎn)我上傳
</div>
<div class="Upload_picture">
<div class="Upload_pictureaduto">
<div class="Upload_picturetitle"><h1>上傳一張圖片</h1><span>X</span></div>
<div class="Upload_picturecenter">
<div class="addbtn">
<span>添加附件</span>
<input id="fileupload" type="file" name="mypic">
</div>
<div class="progress">
<span class="bar"></span><span class="percent">0%</span >
</div>
<div class="files"></div>
<div id="showimg"></div>
</div>
</div>
</div>
</body>
<script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<script src="http://www.helloweba.com/demo/upload/jquery.form.js"></script>
<script type="text/javascript">
$(function () {
var bar = $('.bar');
var percent = $('.percent');
var showimg = $('#showimg');
var progress = $(".progress");
var files = $(".files");
var btn = $(".btn span");
$("#fileupload").wrap("<form id='myupload' action='function.php'method='post' enctype='multipart/form-data'></form>");
$("#fileupload").change(function(){ //選擇文件
$("#myupload").ajaxSubmit({
dataType: 'json', //數(shù)據(jù)格式為json
beforeSend: function() { //開(kāi)始上傳
showimg.empty(); //清空顯示的圖片
progress.show(); //顯示進(jìn)度條
var percentVal = '0%'; //開(kāi)始進(jìn)度為0%
bar.width(percentVal); //進(jìn)度條的寬度
percent.html(percentVal); //顯示進(jìn)度為0%
btn.html("上傳中..."); //上傳按鈕顯示上傳中
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%'; //獲得進(jìn)度
bar.width(percentVal); //上傳進(jìn)度條寬度變寬
percent.html(percentVal); //顯示上傳進(jìn)度百分比
},
success: function(data) { //成功
//獲得后臺(tái)返回的json數(shù)據(jù),顯示文件名,大小,以及刪除按鈕
files.html("<b>"+data.name+"("+data.size+"k)</b><span class='delimg' rel='"+data.pic+"'>刪除</span>");
//顯示上傳后的圖片
var img = "public/"+data.pic;
showimg.html("![]("+img+")");
$('.Alcohol2 a img').attr('src',img);
btn.html("添加附件"); //上傳按鈕還原
},
error:function(xhr){ //上傳失敗
btn.html("上傳失敗");
bar.width('0');
files.html(xhr.responseText); //返回失敗信息
}
});
});
$(document).on("click", ".delimg", function() {
var pic = $(this).attr("rel");
$.post("function.php?act=delimg",{imagename:pic},function(msg){
if(msg==1){
files.html("刪除成功.");
showimg.empty(); //清空?qǐng)D片
progress.hide(); //隱藏進(jìn)度條
}else{
alert(msg);
}
});
});
})
$(document).on("click", ".Alcohol2", function() {
$('.Upload_picture').css('display','block');
})
$(document).on("click", ".Upload_picturetitle span", function() {
$('.Upload_picture').css('display','none');
})
</script>
</html>
function.php :
<?php
@$action = $_GET['act'];
if($action=='delimg'){ //刪除圖片
$filename = $_POST['imagename'];
if(!empty($filename)){
unlink('./public/'.$filename);
echo '1';
}else{
echo '刪除失敗.';
}
}else{ //上傳圖片
$picname = $_FILES['mypic']['name'];
$picsize = $_FILES['mypic']['size'];
if ($picname != "") {
if ($picsize > 512000) { //限制上傳大小
echo '圖片大小不能超過(guò)500k';
exit;
}
$type = strstr($picname, '.'); //限制上傳格式
if ($type != ".gif" && $type != ".jpg" && $type != ".png") {
echo '圖片格式不對(duì)坯沪!';
exit;
}
$rand = rand(100, 999);
$pics = date("YmdHis") . $rand . $type; //命名圖片名稱(chēng)
//上傳路徑
$pic_path = "./public/". $pics;
move_uploaded_file($_FILES['mypic']['tmp_name'], $pic_path);
}
$size = round($picsize/1024,2); //轉(zhuǎn)換成kb
$arr = array(
'name'=>$picname,
'pic'=>$pics,
'size'=>$size
);
echo json_encode($arr); //輸出json數(shù)據(jù)
}
(??)? 到這里就完成了哦,如果此文章對(duì)你有用擒滑,記得要加群哦腐晾,大家一起進(jìn)步交流 。(PHP+JS+UI--交流/招聘 499125737)