圖片上傳插件ImgUploadJS:用HTML5 File API 實現截圖粘貼上傳、拖拽上傳


一 . 背景及效果

當前互聯網上傳文件最多的就是圖片文件了,但是傳統web圖片的截圖上傳需要:截圖保存->選擇路徑->保存后再點擊上傳->選擇路徑->上傳->插入。
圖片文件上傳也需要:選擇路徑再->上傳->插入,步驟繁雜,互聯網體驗為王,如果支持截圖粘貼上傳、拖拽上傳將大大提升體驗。
當前知乎和github對現代瀏覽器均支持這兩種特性,閑來無事就學習實現了一下,今天就說一說這個1kb插件實現什么功能,怎么使用和原理。

首先看一下插效果:

  • 截圖后直接粘貼上傳。

  • 拖拽上傳

http網絡

二.使用示例

直接調用:

<div id="box" style="width: 800px; height: 400px; border: 1px solid;" contenteditable="true"></div>
<script type="text/javascript" src="UploadImage.js"></script>

  new UploadImage("box", "UploadHandler.ashx").upload(function (xhr) {//上傳完成后的回調
                    var img = new Image();
                    img.src = xhr.responseText;
                    this.appendChild(img);
                });

AMD/CMD

<div id="box" style="width: 800px; height: 400px; border: 1px solid;" contenteditable="true"></div>
            <script type="text/javascript" src="require.js"></script>
            <script>
                require(['UploadImage'], function (UploadImage) {

                    new UploadImage("box", "UploadHandler.ashx").upload(function (xhr) {//上傳完成后的回調
                        var img = new Image();
                        img.src = xhr.responseText;
                        this.appendChild(img);
                    });

                })
            </script>

三.瀏覽器支持

當前版本只支持以下,瀏覽器,后期可能會支持更多瀏覽器。

  • IE11
  • Chrome
  • FireFox
  • Safari(未測式,理論應該支持)

四.原理及源碼

  1. 粘貼上傳
    處理目標容器(id)的paste事件,讀取e.clipboardData中的數據,如果是圖片進行以下處理:
    用H5 File API(FileReader)獲取文件的base64代碼,並構建FormData異步上傳。
  2. 拖拽上傳
    處理目標容器(id)的drop事件,讀取e.dataTransfer.files(H5 File API: FileList)中的數據,如果是圖片並構建FormData異步上傳。

以下是初版本代碼,比較簡單。不再贅述。

部份核心代碼

function UploadImage(id, url, key)
    {
        this.element = document.getElementById(id); 
        this.url = url; //后端處理圖片的路徑
        this.imgKey = key || "PasteAreaImgKey"; //提到到后端的name

    }
    UploadImage.prototype.paste = function (callback, formData) 
    {
        var thatthat = this;
        this.element.addEventListener('paste', function (e) {//處理目標容器(id)的paste事件

                if (e.clipboardData && e.clipboardData.items[0].type.indexOf('image') > -1) {
                    var that = this,
                        reader =  new FileReader();
                    file = e.clipboardData.items[0].getAsFile();//讀取e.clipboardData中的數據:Blob對象

                    reader.onload = function (e) { //reader讀取完成后,xhr上傳
                        var xhr = new XMLHttpRequest(),
                            fd = formData || (new FormData());;
                        xhr.open('POST', thatthat.url, true);
                        xhr.onload = function () {
                            callback.call(that, xhr);
                        }
                        fd.append(thatthat.imgKey, this.result); // this.result得到圖片的base64
                        xhr.send(fd);
                    }
                    reader.readAsDataURL(file);//獲取base64編碼
                }
            }, false);
    }

后端及詳細代碼請移步:github:https://github.com/etoah/ImgUploadJS

五. 其它功能

部份功能還在開發中,歡迎提建議或意見。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM