日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
前端進(jìn)階:原生JavaScript實(shí)現(xiàn)具有進(jìn)度監(jiān)聽的文件上傳預(yù)覽組件

本文主要介紹如何使用原生js,通過面向?qū)ο蟮姆绞綄?shí)現(xiàn)一個(gè)文件上傳預(yù)覽的組件,該組件利用FileReader來實(shí)現(xiàn)文件在前端的解析,預(yù)覽,讀取進(jìn)度等功能,并對(duì)外暴露相應(yīng)api來實(shí)現(xiàn)用戶自定義的需求,比如文件上傳,進(jìn)度監(jiān)聽,自定義樣式,讀取成功回調(diào)等。

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作與策劃設(shè)計(jì),嘉善網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:嘉善等地區(qū)。嘉善做網(wǎng)站價(jià)格咨詢:028-86922220

組件設(shè)計(jì)架構(gòu)如下:

涉及的核心知識(shí)點(diǎn)如下:

  1. 閉包:減少變量污染,縮短變量查找范圍
  2. 自執(zhí)行函數(shù)
  3. file API:對(duì)文件進(jìn)行讀取,解析,監(jiān)控文件事件
  4. DocumentFragment API:主要用來優(yōu)化dom操作
  5. minix :用來實(shí)現(xiàn)對(duì)象混合
  6. 正則表達(dá)式:匹配文件類型
  7. class :類組件

github地址

用原生js實(shí)現(xiàn)具有進(jìn)度監(jiān)聽的文件上傳預(yù)覽組件

Demo演示

使用:

 
 
 
 
  • css代碼:

     
     
     
     
    1. .xj-wrap {
    2.             position: relative;
    3.             display: inline-block;
    4.             border: 1px dashed #888;
    5.             width: 200px;
    6.             height: 200px;
    7.             border-radius: 6px;
    8.             overflow: hidden;
    9.         }
    10.         .xj-wrap::before {
    11.             content: '+';
    12.             font-size: 36px;
    13.             position: absolute;
    14.             transform: translate(-50%, -50%);
    15.             left: 50%;
    16.             top: 50%;
    17.             color: #ccc;
    18.         }
    19.         .xj-wrap .xj-pre-img {
    20.             width: 100%;
    21.             height: 100%;
    22.             background-repeat: no-repeat;
    23.             background-position: center center;
    24.             background-size: 100%;
    25.         }
    26.         .xj-file {
    27.             position: absolute;
    28.             left: 0;
    29.             right: 0;
    30.             bottom: 0;
    31.             top: 0;
    32.             opacity: 0;
    33.             cursor: pointer;
    34.         }

    js代碼:

     
     
     
     
    1. (function(win, doc){
    2.     function xjFile(opt) {
    3.         var defaultOption = {
    4.             el: doc.body,
    5.             accept: '*', // 格式按照'image/jpg,image/gif'傳
    6.             clsName: 'xj-wrap',
    7.             beforeUpload: function(e) { console.log(e) },
    8.             onProgress: function(e) { console.log(e) },
    9.             onLoad: function(e) { console.log(e) },
    10.             onError: function(e) { console.error('文件讀取錯(cuò)誤', e) }
    11.         };
    12.         // 獲取dom
    13.         if(opt.el) {
    14.             opt.el = typeof opt.el === 'object' ? opt.el : document.querySelector(opt.el);
    15.         }
    16.         this.opt = minix(defaultOption, opt);
    17.         this.value = '';
    18.         this.init();
    19.     }
    20.     xjFile.prototype.init = function() {
    21.         this.render();
    22.         this.watch();
    23.     }
    24.     xjFile.prototype.render = function() {
    25.         var fragment = document.createDocumentFragment(),
    26.             file = document.createElement('input'),
    27.             imgBox = document.createElement('div');
    28.         file.type = 'file';
    29.         file.accept = this.opt.accept || '*';
    30.         file.className = 'xj-file';
    31.         imgBox.className = 'xj-pre-img';
    32.         // 插入fragment
    33.         fragment.appendChild(file);
    34.         fragment.appendChild(imgBox);
    35.         // 給包裹組件設(shè)置class
    36.         this.opt.el.className = this.opt.clsName;
    37.         this.opt.el.appendChild(fragment);
    38.     }
    39.     xjFile.prototype.watch = function() {
    40.         var ipt = this.opt.el.querySelector('.xj-file');
    41.         var _this = this;
    42.         ipt.addEventListener('change', (e) => {
    43.             var file = ipt.files[0];
    44.             // 給組件賦值
    45.             _this.value = file;
    46.             var fileReader = new FileReader();
    47.             // 讀取文件開始時(shí)觸發(fā)
    48.             fileReader.onloadstart = function(e) {
    49.                 if(_this.opt.accept !== '*' && _this.opt.accept.indexOf(file.type.toLowerCase()) === -1) {
    50.                     fileReader.abort();
    51.                     _this.opt.beforeUpload(file, e);
    52.                     console.error('文件格式有誤', file.type.toLowerCase());
    53.                 }
    54.             }
    55.             // 讀取完成觸發(fā)的事件
    56.             fileReader.onload = (e) => {
    57.                 var imgBox = this.opt.el.querySelector('.xj-pre-img');
    58.                 if(isImage(file.type)) {
    59.                     imgBox.innerHTML = '';
    60.                     imgBox.style.backgroundImage = 'url(' + fileReader.result + ')';
    61.                 } else {
    62.                     imgBox.innerHTML = fileReader.result;
    63.                 }
    64.                 
    65.                 imgBox.title = file.name;
    66.                 this.opt.onLoad(e);
    67.             }
    68.             // 文件讀取出錯(cuò)事件
    69.             fileReader.onerror = (e) => {
    70.                 this.opt.onError(e);
    71.             }
    72.             // 文件讀取進(jìn)度事件
    73.             fileReader.onprogress = (e) => {
    74.                 this.opt.onProgress(e);
    75.             }
    76.             isImage(file.type) ? fileReader.readAsDataURL(file) : fileReader.readAsText(file);
    77.             
    78.         }, false);
    79.     }
    80.     // 清除ipt和組件的值,支持鏈?zhǔn)秸{(diào)用
    81.     xjFile.prototype.clearFile = function() {
    82.         this.opt.el.querySelector('.xj-file').value = '';
    83.         this.value = '';
    84.         return this
    85.     }
    86.     // 簡(jiǎn)單對(duì)象混合
    87.     function minix(source, target) {
    88.         for(var key in target) {
    89.             source[key] = target[key];
    90.         }
    91.         return source
    92.     }
    93.     // 檢測(cè)圖片類型
    94.     function isImage(type) {
    95.         var reg = /(image\/jpeg|image\/jpg|image\/gif|image\/png)/gi;
    96.         return reg.test(type)
    97.     }
    98.     // 將方法掛載到window上
    99.     win.xjFile = xjFile;
    100. })(window, document);

    class版(后期規(guī)劃)

    class版的也很簡(jiǎn)單,大致框架如下,感興趣的朋友可以實(shí)現(xiàn)一下呦~

     
     
     
     
    1. class XjFile {
    2.     constructor(opt) {
    3.     }
    4.     init() {
    5.     }
    6.     watch() {
    7.     }
    8.     render() {
    9.     }
    10.     clearFile() {
    11.     }
    12.     minix(source, target) {
    13.         
    14.     }
    15.     isImage(type) {
    16.         
    17.     }
    18. }

    總結(jié)

    該組件仍有需要完善的地方,在后期使用中,會(huì)慢慢更新,優(yōu)化,歡迎大家提出寶貴的建議。


    網(wǎng)站題目:前端進(jìn)階:原生JavaScript實(shí)現(xiàn)具有進(jìn)度監(jiān)聽的文件上傳預(yù)覽組件
    網(wǎng)站URL:http://www.dlmjj.cn/article/djggegs.html