Image Upload plug-in ImgUploadJS: uses the HTML5 File API to achieve pasting, uploading, and dragging,
I. background and Effects
Currently, the most uploaded files on the Internet are image files, but traditional web images need to be uploaded: save> select path> Save and then click upload> select path> upload> insert.
For Image File Upload, you also need to select a path and then choose upload> insert. The steps are complex and the Internet experience is king. If you support pasting, dragging, and uploading, the experience will be greatly improved.
Currently, zhihu and github both support these two features for modern browsers and learn how to implement the plug-in with nothing to worry about. Today we will talk about the functions, how to use and principles of this 1 kb plug-in.
First, let's take a look at the insert effect:
And then directly paste and upload.
Drag/drop upload
Http Network
Ii. Example
Direct call:
Copy XML/HTML Code to clipboard
- <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) {// callback after the upload is complete
- Var img = new Image ();
- Img. src = xhr. responseText;
- This. appendChild (img );
- });
AMD/CMD
Copy XML/HTML Code to clipboard
- <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) {// callback after the upload is complete
- Var img = new Image ();
- Img. src = xhr. responseText;
- This. appendChild (img );
- });
- })
- </Script>
3. browser support
The current version only supports the following browsers. More browsers may be supported later.
• IE11
• Chrome
• FireFox
• Safari (untested, theory should be supported)
Iv. Principles and source code
1. paste and upload
Process the paste event of the target container (id) and read the data in e. clipboardData. If it is an image, perform the following processing:
Use the H5 File API (FileReader) to obtain the base64 code of the File and construct an asynchronous FormData upload.
2. Drag and Drop upload
Process the drop event of the target container (id), read the data in e. dataTransfer. files (H5 File API: FileList), and construct an asynchronous FormData upload for images.
The following is the first version of the code, which is relatively simple. I will not go into details.
Some core code
Copy XML/HTML Code to clipboard
- Function UploadImage (id, url, key)
- {
- This. element = document. getElementById (id );
- This. url = url; // the path of the backend image.
- This. imgKey = key | "PasteAreaImgKey"; // refer to the backend name.
- }
- UploadImage. prototype. paste = function (callback, formData)
- {
- Var thatthat = this;
- This. element. addEventListener ('paste ', function (e) {// process the paste event of the target container (id)
- If (e. clipboardData & e. clipboardData. items [0]. type. indexOf ('image')>-1 ){
- Var that = this,
- Reader = new FileReader ();
- File = e. clipboardData. items [0]. getAsFile (); // read data in e. clipboardData: Blob Object
- Reader. onload = function (e) {// After reader completes reading, xhr uploads
- 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 obtains the base64 of the image.
- Xhr. send (fd );
- }
- Reader. readAsDataURL (file); // get base64 encoding
- }
- }, False );
- }