JQUERY:[1] Implement picture upload and preview the idea of the principle
1. Gets the URL of the image object to upload when input of the uploaded object is triggered and the local image is selected;
2. Assign the object URL to the SRC attribute of the implementation-written IMG tag
File Object
The file object can be used to obtain information about the files and can also be used to read the contents of the file, typically, the file object is a FileList object returned from the user selecting a file on an INPUT element. It can also be from a DataTransfer object generated by a drag-and-drop operation.
Blob Object
a Blob object is a class file object that contains the raw data that is read-only, and the data in the Blob object does not necessarily have to be native form in JavaScript . The file interface base blob, which inherits the functionality of the BLOB and extends the support for local files on the user's computer, the object URL that we want to get is actually obtained from the Blob object.
Instance Code
1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4 <MetaCharSet= "UTF-8">5 <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0">6 <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge">7 <Scriptsrc= "Jquery-2.1.4.js"></Script>8 <title>Document</title>9 </Head>Ten <Body> One <inputID= "Upload"type= "File"> A <imgID= "Preview"src=""> - <Script> - $('#upload'). Change (function(){ the varobj=document.getElementById ('Upload'). files[0]; - varASRC=window. Url.createobjecturl (obj); - document.getElementById ("Preview"). SRC=asrc; - }) + </Script> - </Body> + </HTML>
Effect
JQUERY:[1] Implement picture upload and preview