溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

html5+PHP怎么實(shí)現(xiàn)無刷新圖片上傳

發(fā)布時(shí)間:2022-04-29 11:08:52 來源:億速云 閱讀:224 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹了html5+PHP怎么實(shí)現(xiàn)無刷新圖片上傳的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇html5+PHP怎么實(shí)現(xiàn)無刷新圖片上傳文章都會(huì)有所收獲,下面我們一起來看看吧。

前端html代碼 upload,html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>上傳</title>
<script type="text/javascript" src="js/jquery.min.js"></script>
<style>
.upload{
   width:300px;
   height:200px;
   position:relative;
}
.upload input[type='file']{
   position:absolute;
   width:70px;
   top:0;
   left:0;
   z-index:10;
   opacity:0;
   filter:alpha(opacity=0);
}
.upload input.selbutton{
   width:70px;
   height:30px;
   background:#cf001b;
   color:#FFF;
   font-size:14px;
   position:absolute;
   top:0;left:0;
   z-index:9;
   border:none;
   cursor:pointer;
}
.upload input.upbutton{
   width:70px;
   height:30px;
   background:#cf001b;
   color:#FFF;
   font-size:14px;
   position:absolute;
   top:50px;left:0;
   z-index:10;
   border:none;
   cursor:pointer;
}
</style>
</head>
<body>
<div class='upload'>
   <input type="file" name="file" />
   <input type="button" name="selbutton" class="selbutton" value="選擇文件" />
   <input type="button" name="upbutton" class="upbutton" value="上傳" />
</div>
<div class='previews'>
   <img src="#" class="image_thumb" alt="圖片預(yù)覽"/>
</div>
</body>
</html>

樣式如下圖

html5+PHP怎么實(shí)現(xiàn)無刷新圖片上傳

接下來是js代碼

<script type="text/javascript">
   $(".upbutton").click(function(){
       //定義允許上傳的圖片格式 在前臺(tái)就可以直接判斷,不合法的格式將不會(huì)上傳
      var filetype = ['jpg','jpeg','png','gif'];
      if($('.image').get(0).files){
            fi = $('.image').get(0).files[0]; //得到文件信息
            //判斷文件格式是否是圖片 如果不是圖片則返回false
            var fname = fi.name.split('.');
            if(filetype.indexOf(fname[1].toLowerCase()) == -1){
                alert('文件格式不支持');
                return ;
            }
            //實(shí)例化h5的fileReader
            var fr = new FileReader();
            fr.readAsDataURL(fi); //以base64編碼格式讀取圖片文件
            fr.onload = function(frev){
                pic = frev.target.result; //得到結(jié)果數(shù)據(jù)
            //開始上傳之前,預(yù)覽圖片
 $('.image_thumb').attr('src',pic);
//使用ajax 利用post方式提交數(shù)據(jù)
                $.post(
                    'handle.php',
                    {
                         message:pic,
                        filename:fname[0],
                        filetype:fname[1],
                        filesize:fi.size
                    },
                    function(data){
                        data = eval('('+data+')');
                        if(data.code == 1 || data.code == 2){
                            console.log('上傳失敗')
                        }else if(data.code == 0){
                            console.log('上傳成功')
                        }
                    }
                );
            }
        }
   })
</script>

接下來是PHP處理代碼  handle.php

$imgtype array(
    'gif'=>'gif',
    'png'=>'png',
    'jpg'=>'jpeg',
    'jpeg'=>'jpeg'
); //圖片類型在傳輸過程中對(duì)應(yīng)的頭信息
$message $_POST['message']; //接收以base64編碼的圖片數(shù)據(jù)
$filename $_POST['filename']; //接收文件名稱
$ftype $_POST['filetype'];  //接收文件類型
//首先將頭信息去掉,然后解碼剩余的base64編碼的數(shù)據(jù)
$message base64_decode(substr($message,strlen('data:image/'.$imgtype[strtolower($ftype)].';base64,')));
$filename $filename.".".$ftype;
$furl = "D:/now/";
//開始寫文件
$file = fopen($furl.$filename,"w");
if(fwrite($file,$message) === false){
    echo json_encode(array('code'=>1,'con'=>'failed'));
    exit;
}
echo json_encode(array('code'=>0,'con'=>$filename));

選擇文件然后點(diǎn)擊上傳的效果如下圖

html5+PHP怎么實(shí)現(xiàn)無刷新圖片上傳

關(guān)于“html5+PHP怎么實(shí)現(xiàn)無刷新圖片上傳”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“html5+PHP怎么實(shí)現(xiàn)無刷新圖片上傳”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI