如何使用jQuery'上传文件;用PHP实现ajax函数?
以下是我的非工作尝试:如何使用jQuery'上传文件;用PHP实现ajax函数?,php,jquery,ajax,file-upload,Php,Jquery,Ajax,File Upload,以下是我的非工作尝试: <script> function uploadImageSubmit() { var imageFile = $('.imageFile').val(); $.ajax({ url: 'ajax.php?request=upload-image&file='+imageFile, success: function(output) {
<script>
function uploadImageSubmit() {
var imageFile = $('.imageFile').val();
$.ajax({
url: 'ajax.php?request=upload-image&file='+imageFile,
success: function(output) {
alert(output);
}
});
}
</script>
<h2>Upload File</h2>
<form>
<input type="file" class="imageFile" />
<a onClick="uploadImageSubmit()">Upload</a>
</form>
函数uploadImageSubmit(){
var imageFile=$('.imageFile').val();
$.ajax({
url:'ajax.php?请求=上传图像和文件='+图像文件,
成功:功能(输出){
警报(输出);
}
});
}
上载文件
上传
“ajax.php”上的代码:
我得到的消息文件没有上传。我认为这是因为即使字符串可以通过url传递,但由于某些原因,文件路径不能。但我也不知道为什么它不起作用。有人能找出问题出在哪里吗?XmlHttpRequest不支持上传文件。您需要使用一些隐藏的iframe或flash解决方案。您不能使用普通的JS/AJAX上传文件。一个已知的技巧是将文件发布到隐藏的iframe并更新iframe。实际上,它支持通过XmlHttpRequest上传。它在Firefox4和Chrome中运行得很好。我认为ajax在文件上传方面遇到了一些问题
<?php
$action = $_GET['request'];
switch($action) {
case 'upload-image':
$imageFile = $_GET['file'];
$name = $_FILES[$imageFile] ['name'];
$tmpLocation = $_FILES[$imageFile] ['tmp_name'];
$upload = move_uploaded_file($tmpLocation, "files/$name");
echo ($upload) ? $name.' uploaded successfully!' : 'File not uploaded.';
end;
}
?>