2012-04-20 141 views
0

我试图将单个文件发送到服务器而无需更改网页。我决定使用这个插件:http://cmlenz.github.com/jquery-iframe-transport/ 我是如何进行的上传很迷茫不改变网页... 的uploadimg.php回报:如何使用ajax上传文件

<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

<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

<?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 .'" />'; 
?> 

回答

0

您没有提交表单,因此'照片'字段不会与AJAX呼叫一起发送。如果您有调试器可用,请检查$ _FILES数组以查看已填充的内容。从this看来,您可能正在寻找'userfile'索引,但我不确定。

0
function upload(event) { 

var formData = new FormData($('#uploadForm')[0]); 

$.ajax({    
    url  : "upload.do", 
    type  : "POST", 
    data : formData, 
    cache : false, 
    contentType  : false, 
    processData : false, 

}); 

@RequestMapping(value = "/uploadCsr", method = RequestMethod.POST) 
    @ResponseBody 
    public String uploadCsr(@RequestParam("fileUpload") MultipartFile file, HttpServletResponse response) { 
// read the file using inputstream 

}