我尝试使用AJAX上传图片。这是我的HTML代码:
<form id='image_form'>
<input placeholder="Write a Comment..." type="text" name="wall_comments" id="wall_comments_<?php echo $mw->wall_id; ?>" value="" style="width:200px;" />
<input id="fileupload" class="comment_image" type="file" name="files" multiple>
<input type="button" name="comment_submit" class="comment_submit" id="<?php echo $mw->wall_id; ?>" value="Submit" />
</form>
我的jQuery代码:
$(document).on("click",".comment_submit",function(){
var fmobj = new FormData();
fmobj.append('files', $('#fileupload').files);
$.ajax({
url: "<?php echo base_url().'/wall/post_comments'; ?>", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: fmobj, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(data) // A function to be called if request succeeds
{
console.log(data);
}
});
});
许多链接都提出了AJAX文件上传的建议。我无法理解的一件事是,在我的控制器中,对于相应的操作,如果我打印$_FILE
我收到:
{"files":[{"name":"index.html","size":0,"url":"http:\/\/teamworks\/team3\/cen\/files\/index.html","deleteUrl":"http:\/\/teamworks\/team3\/cen\/index.php?file=index.html","deleteType":"DELETE"}]}
<pre>Array
(
)
我无法从$_FILE
收到任何内容。许多链接建议在控制器中将文件设置为$_FILE['file']
,但如何在没有$_FILE
的任何内容的情况下继续操作?