我想使用ajax上传文件,但我没有在php $_FILES
中获取数据,我在$_REQUEST
中获取它。我该怎么做。下面是我的jQuery代码.Ajax不能用于文件上传,所以是否有任何代码,以便我可以合并现有的代码来上传文件。Php ajax文件上传错误
<script>
jQuery(function($){
jQuery('#btn').click(function(){
var data = {},
ticks = [];
$('.ajax_elements').each(function(_, elem) {
data[this.id] = this.value;
});
$.ajax({
type : 'POST',
url : 'app_process.php',
data : data,
cache : false
}).done(function(result) {
alert(result);
});
});
});
</script>
<form name="frm" enctype="multipart/form-data">
<input type="text" name="bb" class="ajax_elements" id="one"/>
<input type="file" class="ajax_elements" name="passport" id="passport" />
<input type="button" name="bttn" id="btn" />
</form>
这里是php文件代码
<?php
if($_REQUEST['passport']!=''):
$uploaddir = 'images/';
move_uploaded_file($_FILES["file"]["tmp_name"], $uploaddir . str_replace(" ","_",$_REQUEST['passport']));
endif;
?>
错误消息
通知:未定义指数:文件中 G:\ XAMPP \ htdocs中\ data_ajax \ app_process.php on line
发布错误..无论您得到的是什么 –
您不能使用AJAX上传文件,除非您使用HTML5文件API并发送原始数据或使用隐藏的iframe。 –
是的,你可以。请参阅我的答案与jquery,但也可能没有(请参阅链接@ vijay4vijju的答案) – Asenar