在用戶注冊賬號或者修改資料的時候會需要用戶在本地選擇一張圖片作為頭像,并同時預覽,
常見的思路有兩種:一是將圖片上傳至服務器的臨時文件夾中,并返回該圖片的url,然后渲染在html頁面;另一種思路是,直接在本地內存中預覽圖片,用戶確認提交后再上傳至服務器保存。
這兩種方法各有利弊,方法一很明顯,浪費流量和服務器資源;方法二則加重了瀏覽器的負擔,并且對瀏覽器的兼容性要求更高。
這里介紹的是直接在本地內存中預覽圖片,用戶確認提交后再上傳至服務器保存這種方法
html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
< div class = "reHead" > < P class = "content-format" >頭像支持jpg、png、jpeg格式,文件大小最大不能超過1M</ P > < div class = "content" > < form method = "post" enctype = "multipart/form-data" id = "file_upload" class = "headForm" > < div id = "test-image-preview" class = "iconfont icon-bianjitouxiang" > < input type = "file" name = "test" id = "test-image-file" class = "fileHead" accept = "image/gif, image/jpeg, image/png, image/jpg" multiple = "multiple" > </ div > < div class = "headMain" > < span class = "file" >上傳文件</ span > < p id = "test-file-info" class = "fileName" ></ p > </ div > </ form > </ div > < div class = "but" > < button class = " orangeHead" id = "upImgSub" >< a href = "" title = " rel=" external nofollow" 編輯資料" target = "_blank" >保存</ a ></ button > </ div > </ div > |
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
42
43
44
45
46
47
48
49
50
|
<script type= "text/javascript" src= "./jquery.min.js" ></script> <script> var fileInput = document.getElementById( 'test-image-file' ), info = document.getElementById( 'test-file-info' ), preview = document.getElementById( 'test-image-preview' ); dataBase64 = '' , // preview.style.backgroundImage = 'url(../../img/portrait.png)'; //默認顯示的圖片 // 監聽change事件: fileInput.addEventListener( 'change' , upImg); // 頭像上傳邏輯函數 function upImg(){ preview.style.backgroundImage = '' ; // 清除背景圖片 if (!fileInput.value) { // 檢查文件是否選擇: $( '#test-image-preview' ).addClass( 'icon-bianjitouxiang' ); info.innerHTML = '沒有選擇文件' ; } else { $( '#test-image-preview' ).removeClass( 'icon-bianjitouxiang' ); info.innerHTML = '' ; } var file = fileInput.files[0]; // 獲取File引用 var size = file.size; if (size >= 1 * 1024 * 1024) { //判斷文件大小 info.innerHTML = '文件大于1兆不行!' ; preview.style.backgroundImage = '' ; $( '#test-image-preview' ).addClass( 'icon-bianjitouxiang' ); return false ; } if (file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif' ) { // 獲取File信息: info.innerHTML = '不是有效的圖片文件!' ; preview.style.backgroundImage = '' ; $( '#test-image-preview' ).addClass( 'icon-bianjitouxiang' ); return ; } // 讀取文件: var reader = new FileReader(); reader.onload = function (e) { dataBase64 = e.target.result; // 'data:image/jpeg;base64,/9j/4AAQSk...(base64編碼)...}' preview.style.backgroundImage = 'url(' + dataBase64 + ') ' ; preview.style.backgroundRepeat = 'no-repeat' ; preview.style.backgroundSize = ' 100% 100%' ; }; // 以DataURL的形式讀取文件: reader.readAsDataURL(file); // console.log(file); } |
js 提交頭像到服務器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
$( "#upImgSub" ).click( function () { $.ajax({ type: 'post' , data:{ 'newHead' :dataBase64}, async: false , // 當async屬性的值為false時是同步的,Ajax請求將整個瀏覽器鎖死,只有ajax請求返回結果后,才執行ajax后面的alert語句。 (雖然可行,但是不推薦) // 當async屬性的值為true時是異步的,即不會等待ajax請求返回的結果,會直接執行ajax后面的alert語句。 (后期介紹異步請求解決回地獄) dataType: 'json' , url: '/index/img' , success: function (res) { // 返回成功 if (res.code === 200){ alert(msg) // 上傳成功 } else { alert(msg) // 上傳失敗 } }, error: function () { alert( "接口錯誤" ); // 返回失敗 } }) }); |
當async屬性的值為false時是同步的,Ajax請求將整個瀏覽器鎖死,只有ajax請求返回結果后,才執行ajax后面的alert語句。 (雖然可行,但是不推薦) 當async屬性的值為true時是異步的,即不會等待ajax請求返回的結果,會直接執行ajax后面的alert語句。 (后期介紹異步請求解決回地獄)
css
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
62
63
64
65
66
67
68
69
70
71
72
73
|
body{ font-size : 12px ; } .reHead{ margin : 15px 4% ; } .headForm{ text-align : center ; padding : 40px 0 70px 0 ; } #test-image-preview { position : relative ; display : inline- block ; width : 100px ; height : 100px ; border-radius: 50px ; background : #F5F5F5 ; color : #fff ; font-size : 60px ; text-align : center ; line-height : 100px ; background- size : contain; background-repeat : no-repeat ; background-position : center center ; margin-bottom : 26px ; } .fileHead{ position : absolute ; width : 100px ; height : 100px ; right : 0 ; top : 0 ; opacity: 0 ; } .content- format { font-size : 12px ; font-weight : 400 ; color : rgba( 153 , 153 , 153 , 1 ); } .headMain{ height : 40px ; } .file { position : relative ; background : #fff ; color : #F39800 ; font-weight : 800 ; } .file input { position : absolute ; font-size : 12px ; right : 0 ; top : 0 ; opacity: 0 ; } .fileName { line-height : 28px ; font-size : 12px ; font-weight : 400 ; color : rgba( 51 , 51 , 51 , 1 ); } .but{ text-align : center ; } .orangeHead{ width : 40% ; height : 40px ; background : #f60 ; border : none ; } .orangeHead a{ color : #fff ; } |
以上就是js實現頭像上傳并且可預覽提交的詳細內容,更多關于js 頭像上傳的資料請關注服務器之家其它相關文章!
原文鏈接:https://juejin.cn/post/6909281552438558727