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

一 . 背景及效果

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

首先看一下插效果:html

  • 截圖後直接粘貼上傳。
    java

  • 拖拽上傳
    git

http網絡
github

二.使用示例

直接調用:

<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>

三.瀏覽器支持

當前版本只支持如下,瀏覽器,後期可能會支持更多瀏覽器。web

  • 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瀏覽器

五. 其它功能

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

相關文章
相關標籤/搜索