本篇向大家介紹一種全新的上傳圖片的方式,利用html5的filereader讀取圖片文件,然后將數(shù)據(jù)傳輸?shù)椒?wù)器再使用php進行處理。實現(xiàn)過程如下(帶圖片預(yù)覽功能)
前端html代碼 upload,html
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
|
<!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> |
樣式如下圖
接下來是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
39
40
41
|
<script type= "text/javascript" > $( ".upbutton" ).click( function (){ //定義允許上傳的圖片格式 在前臺就可以直接判斷,不合法的格式將不會上傳 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 ; } //實例化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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
$imgtype = array ( 'gif' => 'gif' , 'png' => 'png' , 'jpg' => 'jpeg' , 'jpeg' => 'jpeg' ); //圖片類型在傳輸過程中對應(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 )); |
選擇文件然后點擊上傳的效果如下圖
以上就是整個圖片上傳的代碼。當然對于php的部分還有很多可以優(yōu)化的地方,比如文件名部分,可以重命名,以保證相同文件名上傳以后的文件名是不同的等等。這種上傳方式我也是剛開始使用,當初是受node.js做上傳的的啟發(fā),然后嘗試著應(yīng)用于php,沒想到還真能上傳成功。
希望大家也可以按照此方法實現(xiàn)圖片上傳。