<form method="POST" action="upload" enctype="multipart/form-data"> 名字 <input type="text" name="user"></input> 头像 <input type="file" name="file"></input> <input type="submit" id="_submit" value="提交"></input> </form>
document.getElementById("_submit").onclick = function(event){
//取消掉默认的form提交方式
if(event.preventDefault) event.preventDefault();
else event.returnValue = false; //对于IE的取消方式
var formDOM = document.getElementsByTagName("form")[];
//将form的DOM对象当作FormData的构造函数
var formData = new FormData(formDOM);
var req = new XMLHttpRequest();
req.open("POST", "upload");
//请求完成
req.onload = function(){
if(this.status === ){
//对请求成功的处理
}
}
//将form数据发送出去
req.send(formData);
//避免内存泄漏
req = null;
}
var img = document.createElement("img");
img.src = JSON.parse(this.responseText).path;
formDOM.insertBefore(img, document.getElementById("_submit"));
var req = new XMLHttpRequest();
var sendData = "user=abc&file=这是一个文本文件的内内容";
req.open("POST", "upload");
//发送的数据需要转义,见上面提到的三种格式
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.send(sendData);
var iframe = document.createElement("iframe");
iframe.width = 0;
iframe.height = 0;
iframe.border = 0;
iframe.name = "form-iframe";
iframe.id = "form-iframe";
iframe.setAttribute("style", "width:0;height:0;border:none");
//放到document
this.form.appendChild(iframe);
this.form.target = "form-iframe";
iframe.onload = function(){
var img = document.createElement("img");
//获取iframe的内容,即服务返回的数据
var responseData = this.contentDocument.body.textContent || this.contentWindow.document.body.textContent;
img.src = JSON.parse(responseData).path;
f.insertBefore(img, document.getElementById("_submit"));
//删掉iframe
setTimeout(function(){
var _frame = document.getElementById("form-iframe");
_frame.parentNode.removeChild(_frame);
}, 100);
//如果提示submit函数不存在,请注意form里面是否有id/value为submit的控件
this.form.submit();
}
var req = new XMLHttpRequest();
req.open("POST", "upload");
//设置和邮箱一样的Content-Type
req.setRequestHeader("Content-Type", "application/octet-stream");
var fr = new FileReader();
fr.onload = function(){
req.sendAsBinary(this.result);
}
req.onload = function(){
//一样,省略
}
//读取input文件内容,放到fileReader的result字段里
fr.readAsBinaryString(this.form["file"].files[0]);
if(typeof XMLHttpRequest.prototype.sendAsBinary === 'undefined'){
XMLHttpRequest.prototype.sendAsBinary = function(text){
var data = new ArrayBuffer(text.length);
var uia = new UintArray(data, );
for (var i = ; i < text.length; i++){
uia[i] = (text.charCodeAt(i) & xff);
}
this.send(uia);
}
}
form.html <form enctype="multipart/form-data" method="post" target="upload" action="upload.php" > <input type="file" name="uploadfile" /> <input type="submit" /> </form> <iframe name="upload" style="display:none"></iframe>
-->
upload.php
<?php
header("Content-type:text/html;charset=utf-");
class upload{
public $_file;
public function __construct(){
if(!isset($_FILES['uploadfile'])){
$name=key($_FILES);
}
if(!isset($_FILES['uploadfile'])){
throw new Exception("并没有文件上传");
}
$this->_file=$_FILES['uploadfile']; //$this->_file一维数组
var_dump($this->_file);
//判断文件是否是通过 HTTP POST 上传的
//如果 filename 所给出的文件是通过 HTTP POST 上传的则返回 TRUE。这可以用来确保恶意的用户无法欺骗脚本去访问本不能访问的文件,例如 /etc/passwd。
if(!is_uploaded_file($this->_file['tmp_name']))
throw new Exception("异常情况");
if($this->_file['error'] !== )
throw new Exception("错误代码:".$this->_file['error']);
}
public function moveTo($new_dir){
$real_dir=$this->checkDir($new_dir).'/';
$real_dir=str_replace("\\","/",$real_dir);
if(!move_uploaded_file($this->_file['tmp_name'],$real_dir.$this->_file['name'])){
exit('上传失败');
}
echo "<script type='text/javascript'>alert('上传成功')</script>";
}
public function checkDir($dir){
if(!file_exists($dir)){
mkdir($dir,,true);
}
return realpath($dir);
}
}
$upload=new upload();
$new_dir="./a/b";
$upload->moveTo($new_dir);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有