Table of Contents
Html5上传文件测试,带进度条
Home Web Front-end H5 Tutorial Use Html5 to implement asynchronous file upload, support cross-domain, and upload progress bar

Use Html5 to implement asynchronous file upload, support cross-domain, and upload progress bar

Mar 24, 2017 pm 03:27 PM
html5 upload files asynchronous Cross domain progress bar

The following editor will bring you an article that uses Html5 to upload files asynchronously, supports cross-domain, and has an upload progress bar. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

Server preparation IIS

You need to set the HTTP response header in IIS, as shown in the figure, add the following settings , add this "Access-Control-Allow-Origin". Only by adding this line can you support cross-domain, otherwise Chrome browser will report an error

Use Html5 to implement asynchronous file upload, support cross-domain, and upload progress bar

Use Html5 to implement asynchronous file upload, support cross-domain, and upload progress bar

Page code:


XML/HTML CodeCopy content to clipboard

<!DOCTYPE html>  
<html>  
<head>  
    <meta http-equiv="content-type" content="text/html;charset=utf-8">  
    <meta name="format-detection" content="telephone=no">  
    <meta name="msapplication-tap-highlight" content="no">  
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">  
    <title>Html5上传文件</title>  
</head>  
<body>  
    <p class="app">  
        <h1 id="Html-上传文件测试-带进度条">Html5上传文件测试,带进度条</h1>  
        <p>  
            <input type="file" value="" id="fileInput" name="files" onchange="fileSelected()" />  
            <p style="margin:30px;">  
                <input type="button" value="上传" onclick="uploadFile()" />  
            </p>  
            <p style="margin:30px;">  
                <p id="fileName"></p>  
                <p id="fileSize"></p>  
                <p id="fileType"></p>  
            </p>  
            <p style="margin:30px;width:500px;height:15px;border:1px solid #aeaeae;">  
                <p id="progress" style="background:#4cff00;height:15px;width:0%;"></p>  
                <p id="percentNumber"></p>  
            </p>  
            <p style="margin:30px;">  
                <p id="msg"></p>  
            </p>  
        </p>  
    </p>  
    <script type="text/javascript">  
        function fileSelected() {   
            //重置状态显示   
            document.getElementById("msg").innerHTML = "";   
            document.getElementById(&#39;percentNumber&#39;).innerHTML = &#39;&#39;;   
            document.getElementById("progress").style.width = "0%";   
            var file = document.getElementById(&#39;fileInput&#39;).files[0];   
            if (file) {   
                var fileSize = 0;   
                if (file.size > 1024 * 1024)   
                    fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + &#39;MB&#39;;   
                else   
                    fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + &#39;KB&#39;;   
                document.getElementById(&#39;fileName&#39;).innerHTML = &#39;Name: &#39; + file.name;   
                document.getElementById(&#39;fileSize&#39;).innerHTML = &#39;Size: &#39; + fileSize;   
                document.getElementById(&#39;fileType&#39;).innerHTML = &#39;Type: &#39; + file.type;   
            }   
        }   
  
  
        function uploadFile() {   
            var fd = new FormData();   
            fd.append("fileInput", document.getElementById(&#39;fileInput&#39;).files[0]);   
            var xhr = new XMLHttpRequest();   
            xhr.upload.addEventListener("progress", uploadProgress, false);   
            xhr.addEventListener("load", uploadComplete, false);   
            xhr.addEventListener("error", uploadFailed, false);   
            xhr.addEventListener("abort", uploadCanceled, false);               
            xhr.open("POST", "http://10.0.0.200:9001/Home/Upload");//修改为自己服务器接口地址   
            //xhr.setRequestHeader("Access-Control-Allow-Origin", "*");//需要在IIS里面配置,就可以跨域请求了   
            //xhr.setRequestHeader("Content-Type", "multipart/form-data");   
            xhr.send(fd);   
        }   
        function uploadProgress(evt) {   
            if (evt.lengthComputable) {   
                var percentComplete = Math.round(evt.loaded * 100 / evt.total);   
                document.getElementById(&#39;percentNumber&#39;).innerHTML = percentComplete + &#39;%&#39;;   
                var jindutiao = document.getElementById("progress");   
                jindutiao.style.width = percentComplete + "%";   
            }   
            else {   
                document.getElementById(&#39;percentNumber&#39;).innerHTML = &#39;不支持进度计算&#39;;   
            }   
        }   
        function uploadComplete(evt) {   
            //evt.target.responseText   
            document.getElementById("msg").innerHTML = "上传成功";   
        }   
        function uploadFailed(evt) {   
            document.getElementById("msg").innerHTML = "上传过程中有一个错误";   
        }   
        function uploadCanceled(evt) {   
            document.getElementById("msg").innerHTML = "用户取消了上传或者浏览器删除了连接";   
        }   
    </script>  
</body>  
</html>
Copy after login

The above article uses Html5 to implement asynchronous file upload, supports cross-domain, and has an upload progress bar. This is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support PHP. Chinese website.

For more related articles using Html5 to implement asynchronous file upload, support cross-domain, and upload progress bar, please pay attention to the PHP Chinese website!

Related articles:

How to obtain thinkphp3.2.3 upload file path

Thinkphp3.2.3 sample code for integrating phpqrcode to generate QR code Share

PHP realizes file upload without page refresh

Uses Html5 to realize asynchronous file upload, supports cross-domain, and has upload progress bar

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles