JS+html5实现异步上传图片显示上传文件进度条功能示例
人气:0本文实例讲述了JS+html5实现异步上传图片显示上传文件进度条功能。分享给大家供大家参考,具体如下:
<html> <head> </head> <body> <p> emo_album_id:<input type="text" name="emo_album_id" id="emo_album_id" value='1'> </p> <p> name:<input type="text" name="title" id="title"> </p> <div class="row"> <label for="file"> Upload Image:</label> <input type="file" name="fileToUpload" id="fileToUpload" multiple="multiple" onchange="fileSelected();" /> </div> SentenceMovie[photo] <div id="fileName"> </div> <div id="fileSize"> </div> <div id="fileType"> </div> <div id="progressNumber"> </div> <script> function fileSelected() { var file = document.getElementById('fileToUpload').files[0]; if (file) { var fileSize = 0; if (file.size > 1024 * 1024) fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB'; else fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB'; document.getElementById('fileName').innerHTML = 'Name: ' + file.name; document.getElementById('fileSize').innerHTML = 'Size: ' + fileSize; document.getElementById('fileType').innerHTML = 'Type: ' + file.type; uploadFile(); } } function uploadFile() { var fd = new FormData(); fd.append("upload_file", document.getElementById('fileToUpload').files[0]); fd.append("emo_album_id", document.getElementById('emo_album_id').value); fd.append("title", document.getElementById('title').value); 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://mysae.com/emotions/1/api/index.php/emo/upload"); xhr.send(fd); } function uploadProgress(evt) { if (evt.lengthComputable) { var percentComplete = Math.round(evt.loaded * 100 / evt.total); document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%'; } else { document.getElementById('progressNumber').innerHTML = 'unable to compute'; } } function uploadComplete(evt) { /* This event is raised when the server send back a response */ alert(evt.target.responseText); } function uploadFailed(evt) { alert("There was an error attempting to upload the file."); } function uploadCanceled(evt) { alert("The upload has been canceled by the user or the browser dropped the connection."); } </script> </body> </html>
希望本文所述对大家JavaScript程序设计有所帮助。
您可能感兴趣的文章:
- vue中封装axios并实现api接口的统一管理
- vue3.0 的 Composition API 的使用示例
- 详解Vue中Axios封装API接口的思路及方法
- vue项目打包为APP,静态资源正常显示,但API请求不到数据的操作
- Vue 使用typescript如何优雅的调用swagger API
- vue设置全局访问接口API地址操作
- Springboot项目使用html5的video标签完成视频播放功能
- vue中解决微信html5原生ios虚拟键返回不刷新问题
- javascript实现移动端 HTML5 图片上传预览和压缩功能示例
- JS+HTML5本地存储Localstorage实现注册登录及验证功能示例
- 如何在vue中使用HTML 5 拖放API
加载全部内容