Html5 upload pictures, common code sharing for mobile and PC terminals
This article mainly introduces the relevant information about the general code of Html5 uploading pictures on mobile and PC. It is very good and has reference value. Friends who need it can refer to it.
No more nonsense, let’s talk about it. What I want to share with you today is to upload images using HTML5. We use it on the mobile terminal, but this is also universally compatible on PC. I have only tested it on Google. "Using HTML5's File API to preview uploaded images" was written in angular. Today I will abandon the angular stuff and share an html5 + js image upload case. So let’s follow certain steps today.
HTML The first step is to create html. We place an input#upload for file selection on the page (PS: I’m lazy and won’t repeat it here. I copied our page directly when writing the case)
<p class="con4"> <span class="btn upload">上传<input type="file" class="upload_pic" id="upload" /></span> </p>
CSS Note: The css is a bit messy, so if you don’t understand it, it’s okay Just ask me, or just write it yourself.
CSS Code复制内容到剪贴板 con{ width: %; height: auto; overflow: hidden; margin: % auto auto; color: #FFFFFF; } con .btn{ width: %; height: px; line-height: px; text-align: center; background: #dbc; display: block; font-size: px; border-radius: px; } upload{ float: left; position: relative; } upload_pic{ display: block; width: %; height: px; position: absolute; left: ; top: ; opacity: ; border-radius: px; }
Get the node through getElementById to determine the browser compatibility. For those that do not support FileReaderinterface The browser will give a prompt and disable the input, otherwise listen for the input's change event.
JavaScript Code复制内容到剪贴板 //获取上传按钮 var input = document.getElementById("upload"); if(typeof FileReader==='undefined'){ //result.innerHTML = "抱歉,你的浏览器不支持 FileReader"; input.setAttribute('disabled','disabled'); }else{ input.addEventListener('change',readFile,false); }
Then, when the change event of file_input is triggered, call the function readFile(). In readFile, we first get the file object, and then detect the file type through the file's type attribute. Of course, we only allow the selection of image type files, and then wenewA FileReader instance, and call the readAsDataURL method to read the selected image file. Finally, in the onload event, obtain the successfully read file content and display the selected image file by inserting an img node. picture.
JavaScript Code复制内容到剪贴板 function readFile(){ var file = this.files[]; if(!/image\/\w+/.test(file.type)){ alert("文件必须为图片!"); return false; } var reader = new FileReader(); reader.readAsDataURL(file); //当文件读取成功便可以调取上传的接口,想传哪里传哪里(PS: 你们可以把你们的靓照偷偷发给我!) reader.onload = function(e){ var data = this.result.split(','); var tp = (file.type == 'image/png')? 'png': 'jpg'; var a = data[]; //需要上传到服务器的在这里可以进行ajax请求 ... ... } };
As of this writing, we have completed the image upload function. If you are interested, try it yourself. If you don’t understand anything or I made a mistake, please contact me. FileReader methods and events
The above is the detailed content of Html5 upload pictures, common code sharing for mobile and PC terminals. For more information, please follow other related articles on the PHP Chinese website!

H5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.

HTML5 code consists of tags, elements and attributes: 1. The tag defines the content type and is surrounded by angle brackets, such as. 2. Elements are composed of start tags, contents and end tags, such as contents. 3. Attributes define key-value pairs in the start tag, enhance functions, such as. These are the basic units for building web structure.

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)