PHP Ajax文件异步上传代码(XMLHttpRequest)例子
发布:smiling 来源: PHP粉丝网 添加日期:2016-01-20 16:08:06 浏览: 评论:0
文件异步上传通常是通过ajax实现了,也有朋友说使用iframe来实现这个虽然说可以达到目的但不是真正的a异步上传了并且如果浏览器不支持irame那么iframe模仿上传就无效了,下面我们来看一段真正的文件异步上传例子.
PHP 代码:
- $fileName = $_FILES['afile']['name'];
- $fileType = $_FILES['afile']['type'];
- $fileContent = file_get_contents($_FILES['afile']['tmp_name']);
- $dataUrl = 'data:' . $fileType . ';base64,' . base64_encode($fileContent);
- $json = json_encode(array(
- 'name' => $fileName,
- 'type' => $fileType,
- 'dataUrl' => $dataUrl,
- 'username' => $_REQUEST['username'],
- 'accountnum' => $_REQUEST['accountnum']
- ));
- echo $json;
- Html 及 JS 代码
- <!DOCTYPE html>
- <!--
- Copyright 2012 Google Inc.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- Author: Eric Bidelman (ericbidelman@chromium.org)
- -->
- <html>
- <head>
- <meta charset="utf-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
- <title>xhr.send(FormData) Example</title>
- </head>
- <body>
- <input type="file" name="afile" id="afile" accept="image/*"/>
- <script>
- document.querySelector('#afile').addEventListener('change', function(e) {
- var file = this.files[0];
- var fd = new FormData();
- fd.append("afile", file);
- // These extra params aren't necessary but show that you can include other data.
- fd.append("username", "Groucho");
- fd.append("accountnum", 123456);
- var xhr = new XMLHttpRequest();
- xhr.open('POST', 'handle_file_upload.php', true);
- xhr.upload.onprogress = function(e) {
- if (e.lengthComputable) {
- var percentComplete = (e.loaded / e.total) * 100;
- console.log(percentComplete + '% uploaded');
- }
- };
- xhr.onload = function() {
- if (this.status == 200) {
- var resp = JSON.parse(this.response);
- console.log('Server got:', resp);
- var image = document.createElement('img');
- image.src = resp.dataUrl;
- document.body.appendChild(image);
- };
- };
- xhr.send(fd);
- }, false);
- </script>
- <!--[if IE]>
- <script src="http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js"></script>
- <script>CFInstall.check({mode: 'overlay'});</script>
- <![endif]-->
- </body>
- </html>
Tags: PHP Ajax异步上传 XMLHttpRequest
相关文章
- ·PHP中通过Web 执行C/C++应用程序(2013-11-13)
- ·用PHP实现Ftp用户的在线管理(2013-11-13)
- ·用PHP自动把纯文本转换成Web页面(2013-11-13)
- ·用实例分析PHP5异常处理(2013-11-13)
- ·php5的simplexml解析错误(2013-11-13)
- ·PHP后门的隐藏技巧测试报告(2013-11-13)
- ·PHP缓存技术详谈(2013-11-27)
- ·利用PHP自定义错误处理器处理出错信息(2013-11-27)
- ·PHP作wap开发时遇到的问题(2013-11-27)
- ·php编写大型网站问题集(2013-11-27)
- ·php测试性能代码(2013-11-28)
- ·php 安全register globals设置为TRUE的危害(2013-11-28)
- ·XSLTProcessor 中 registerPHPFunctions 后无法调用 php 函数(2013-11-30)
- ·PHP中常用三种缓存技术(2013-11-30)
- ·新浪微博PHP版SDK的导致20007错误(2013-12-03)
- ·linux中phpMyAdmin错误提示Wrong permissions on configuration file, should no(2013-12-04)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)