如何使用ajax上传文件
我试图将单个文件发送到服务器,而不必更改网页.我决定使用此插件: http://cmlenz.github.com/jquery-iframe-transport / 我对如何继续上传而不更改网页感到非常困惑... uploadimg.php返回:
I was trying to send a single file to the server without having to change the webpage. I decided to use this plugin: http://cmlenz.github.com/jquery-iframe-transport/ I am very confused on how to proceed for an upload without changing the webpage... the uploadimg.php returns:
<br />
<b>Notice</b>: Undefined index: photo in <b>C:\website\uploadimg.php</b> on line <b>2</b><br />
<br />
<b>Notice</b>: Undefined index: photo in <b>C:\website\uploadimg.php</b> on line <b>3</b><br />
<img src="userimg/" />
我的文件:
postAd.php
postAd.php
<html>
<head>
<script src="jquery.js"></script>
<script src="jquery-iframe-transport.js"></script>
<script>
$(document).ready(function(){
$('#photo1').on('change', function(){
$.ajax({
url: "uploadimg.php",
files: $("#photo1:file"),
iframe: true,
processData: false
}).done(function(data){
console.log(data);
});
});
});
</script>
</head>
<body>
<input type='file' name='photo' id='photo1' /></br>
</body>
</html>
uploadimg.php
uploadimg.php
<?php
$file_tmp = $_FILES['photo']['tmp_name'][0];
$file = $_FILES['photo']['name'][0];
move_uploaded_file($file_tmp, "userimg/$file");
echo '<img src="userimg/' .$file .'" />';
?>
您没有提交表单,因此不会通过AJAX调用发送照片"字段.如果有可用的调试器,请检查$ _FILES数组以查看已填充的内容.从此看来,您可能是寻找用户文件"索引,但我不确定.
You are not submitting a form, so the 'photo' field is not going to be sent with the AJAX call. If you have a debugger available, check the $_FILES array to see what got populated. From this, it appears that you might be looking for a 'userfile' index, but I'm not sure.