我想使用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