急!ajax上传图片,PHP获取不到 5
ajax上传formdata,PHP端接收不到数据,打印数组是空的代码如下:<div><formaction=""id="formdata"enctype="multip...
ajax上传formdata,PHP端接收不到数据,打印数组是空的
代码如下:
<div>
<form action="" id="formdata" enctype="multipart/form-data" method="post">
<div id="upload" class="btn btn-primary">选择图片</div>
<div class="btn btn-success" id="uploadImg">上传</div>
<input id="file" type="file" multiple style="display:none">
<div class="gallery" id="gallery"></div>
</form>
</div>
<script>
$("#uploadImg").click(function(){
var files = that.files;
var uploadFile = new FormData($("#formdata")[0]);
for(var i=0;i<files.length;i++){
uploadFile.append('imgs[]',files[i]);
}
if("undefined" != typeof(uploadFile) && uploadFile != null && uploadFile != ""){
alert("1");
$.ajax({
url:'uploadimg/upimg.php',
type:'POST',
data:uploadFile,
//async: false,
cache: false,
contentType: false, //不设置内容类型
processData: false, //不处理数据
success:function(data){
alert(data);
},
error:function(){
alert("上传失败!");
}
})
}else {
}
})
</script>
php代码:
<?php
$files=$_FILES['uploadFile'];
echo 'FILES:'.var_dump($_FILES);
echo 'POST:'.var_dump($_POST);
?>
输出结果:
<br />
<b>Notice</b>: Undefined index: uploadFile in <b>C:\phpStudy\WWW\wx-xcx\admin\en\uploadimg\upimg.php</b> on line <b>3</b><br />
array(0) {
}
FILES:array(0) {
}
POST: 展开
代码如下:
<div>
<form action="" id="formdata" enctype="multipart/form-data" method="post">
<div id="upload" class="btn btn-primary">选择图片</div>
<div class="btn btn-success" id="uploadImg">上传</div>
<input id="file" type="file" multiple style="display:none">
<div class="gallery" id="gallery"></div>
</form>
</div>
<script>
$("#uploadImg").click(function(){
var files = that.files;
var uploadFile = new FormData($("#formdata")[0]);
for(var i=0;i<files.length;i++){
uploadFile.append('imgs[]',files[i]);
}
if("undefined" != typeof(uploadFile) && uploadFile != null && uploadFile != ""){
alert("1");
$.ajax({
url:'uploadimg/upimg.php',
type:'POST',
data:uploadFile,
//async: false,
cache: false,
contentType: false, //不设置内容类型
processData: false, //不处理数据
success:function(data){
alert(data);
},
error:function(){
alert("上传失败!");
}
})
}else {
}
})
</script>
php代码:
<?php
$files=$_FILES['uploadFile'];
echo 'FILES:'.var_dump($_FILES);
echo 'POST:'.var_dump($_POST);
?>
输出结果:
<br />
<b>Notice</b>: Undefined index: uploadFile in <b>C:\phpStudy\WWW\wx-xcx\admin\en\uploadimg\upimg.php</b> on line <b>3</b><br />
array(0) {
}
FILES:array(0) {
}
POST: 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询