gpt4 book ai didi

javascript - codeigniter 获取 ajax post 文件

转载 作者:可可西里 更新时间:2023-10-31 23:38:31 27 4
gpt4 key购买 nike

我正在尝试在 codeigniter 中使用 ajax 上传多个文件。我的 ajax 帖子工作正常,但我不知道如何在我的 Controller 中获取帖子文件。
我试过这种方式,但它不起作用。请帮忙。谢谢。

HTML

                    <form method="post" name="upload_files" action="<?php echo base_url(); ?>home/upload_files" id="form_files" enctype="multipart/form-data">
<h4 style="float: left">Add files. (file types .png, .jpeg, .dwg, .pdf, jpg) max: 10Mb <span style="color:red;" id ="err_type"> * </span></h4>
<div class=from-section>
<div class="row">
<div class="span4" id="image" >

<div class="cart-total" id="clone_image" data-provides="fileupload">
<div class="form-group has-success has-feedback">
<input class="form-control" type="file" name="files[]" accept="image/*" />
</div>
</div>

</div><!-- /.span4 -->
</div><!-- /.row -->
</div>

<div class="cart-total">
<a id="another_image" class="btn btn-default" style="height: 45px;padding: 12px 12px; margin-right: 10px; ">+ Add More Files</a>
<a id="submit_files" class="rx-bottom" >Submit Files</a>

</div>


</form>

AjAX

    <script type="text/javascript">


$('#submit_files').click(function() {

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

$.ajax({
url: "<?php echo base_url('home/upload_files/'); ?>",
type: "POST",
data: formData,
mimeType: "multipart/form-data",
processData: false,
contentType: false,
success: function(res) {
console.log("success");
},
error: function(res) {
console.log("error");
}
});

});

</script>

我的控制台正在记录成功消息

Controller

    public function upload_files(){

$order_id = 123;

if(!$order_id){
redirect('home/instant_quote', 'refresh');
}else{

$this->load->library('upload');
//$files = $this->input->post();
$files = $_FILES;

if($files){
for( $i = 1; $i < 20; $i++) {

$config = array();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|jpeg|png|pdf|dwg|zip';
$config['max_size'] = '11000';
$config['file_name'] = 'order_'.$order_id.'_'.$i;
$config['overwrite'] = false;

$this->upload->initialize($config);

$this->upload->do_upload('files'.$i);

$image_data = array('upload_data' => $this->upload->data());

if($image_data["upload_data"]["file_size"]){
$inserted['order'] = $i ;
$inserted['name'] = $image_data["upload_data"]["file_name"];
$inserted['extension'] = $image_data["upload_data"]["file_ext"];
$inserted['type'] = $image_data["upload_data"]["file_type"];
$inserted['size'] = $image_data["upload_data"]["file_size"];

$prop = $this->home->insert_files($inserted);
}
}
}else{
$this->load->view('comman/header.php');
$this->load->view('home/add_files.php');
$this->load->view('comman/footer.php');
}

}
}

这是我记录表单数据时看到的内容

enter image description here

最佳答案

你可以在你的 Controller 中使用它:

function upload(){
if(!empty($_FILES)){
$type= $_FILES["files0"]["type"]; //files0 is your input file name
$size= $_FILES["files0"]["size"];

$file_name = $_FILES["files0"]["name"];
$source = $_FILES['files0']['tmp_name'];
$dir = "./uploads/";
$file = $dir . $file_name;
if(!file_exists ($file ))
{
move_uploaded_file($source,$file );
exit("File has been uploaded");
}
else
{
echo "File exist";
}
}
$this->load->view('v_test');
}

对于您的 ajax jquery:

//add this to your head html
<script>
var base_url = "<?php echo base_url(); ?>";
</script>

并且,这是您的 ajax 调用:

$("#submit_files").click(function() {
var formData = new FormData($("#form_files")[0]);

$.ajax({
url: base_url + "home/upload_files",
type: 'POST',
data: formData,
async: false,
success: function (data) {
alert(data)
},
cache: false,
contentType: false,
processData: false
});

return false;
});

请先测试单个文件,看看它是否有效。尝试动态上传多个文件。希望这能帮助你。 :D

关于javascript - codeigniter 获取 ajax post 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34114784/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com