<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>FormData
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-CN" />
<script type="text/javascript" src="http://files.cnblogs.com/Zjmainstay/jquery-1.6.2.min.js"></script>
</head>
<body>
<style>
#feedback{width:1200px;margin:0 auto;}
#feedback img{float:left;width:300px;height:300px;}
</style>
<div>
<!-- 点击图片添加文件方式 -->
<img src="http://f7-preview.awardspace.com/zjmainstay.co.cc/jQueryExample/jquery_upload_image/files/addfile.jpg" onclick="getElementById('inputfile').click()" title="点击添加图片" alt="点击添加图片">
<input type="file" name="image" style="opacity:0;filter:alpha(opacity=0);" id="inputfile"/>
</div>
<div id="feedback"></div> <!-- 响应返回数据容器 -->
<script type="text/javascript">
$(document).ready(function(){
$("#inputfile").change(function(){
//创建FormData对象
var data = new FormData();
//为FormData对象添加数据
//
$.each($('#inputfile')[0].files, function(i, file) {
data.append('upload_file', file);
});
$.ajax({
url:'submit_form_process.php',
type:'POST',
data:data,
cache: false,
contentType: false, //不可缺
processData: false, //不可缺
success:function(data){
data = $(data).html();
if($("#feedback").children('img').length == 0) $("#feedback").append(data.replace(/</g,'<').replace(/>/g,'>'));
else $("#feedback").children('img').eq(0).before(data.replace(/</g,'<').replace(/>/g,'>'));
}
});
});
});
</script>
</body>
</html>
特别的:contentType: false, processData: false,
这两个参数是必须的。
缺少contentType: false,$_FILES值为空。
缺少processData: false,FF控制台报错:“NS_ERROR_XPC_BAD_OP_ON_WN_PROTO: Illegal operation on WrappedNative prototype object”,直接不能运行。
后台:var_dump($_FILSE);
转载于:https://www.cnblogs.com/hgj123/p/4848650.html