在这里,我想使用jquery ajax函数上传和移动csv文件。
$("#file").change(function() {
alert($("#file").val());
$.ajax({
url: "<?php echo base_url(); ?>index.php/Task_controller/upload_tasksquestion",
type: "post",
dataType: 'json',
processData: false,
contentType: false,
data: {file: $("#file").val()},
success: function(text) {
alert(text);
if(text == "success") {
alert("Your image was uploaded successfully");
}
},
error: function() {
alert("An error occured, please try again.");
}
});
});
我的html代码是:
< input type="file" class="form-control" id="file" name="file">
我在控制器函数中测试上传的文件名,比如$this->input->post(' file ');
但是它没有到达控制器,并且我想将该文件移动到一个文件夹中。请帮帮我,我哪里做错了…
我的服务器端代码是:
$curdate=date('Y-m-d');
$path='./assets/fileuploads/';
$file=basename($_FILES['file']['name']);
list($txt, $ext) = explode(".", $file);
$actual_file_name = time().substr($txt, 5).".".$ext;
if(move_uploaded_file($_FILES['file']['tmp_name'],$path.$actual_file_name))
发布于 2017-03-18 20:30:03
您需要使用FormData通过AJAX发送文件。
将文件存储在FormData对象中,并将该对象作为data
传递。
$("#file").change(function() {
var fd = new FormData();
fd.append('file', this.files[0]); // since this is your file input
$.ajax({
url: "<?php echo base_url(); ?>index.php/Task_controller/upload_tasksquestion",
type: "post",
dataType: 'json',
processData: false, // important
contentType: false, // important
data: fd,
success: function(text) {
alert(text);
if(text == "success") {
alert("Your image was uploaded successfully");
}
},
error: function() {
alert("An error occured, please try again.");
}
});
});
阅读File Uploading Class (特别是在控制器部分),了解如何在服务器端处理该文件。
https://stackoverflow.com/questions/42874303
复制相似问题