【问题标题】:Getting error 'You did not select file' codeigniter [duplicate]收到错误'您没有选择文件'codeigniter [重复]
【发布时间】:2017-06-20 04:45:53
【问题描述】:

我在 codeigniter 中收到错误“您没有选择要上传的文件”。不要我做错了什么,这是我的代码。

查看

<form action="#" method="post" id="svf-form-4" enctype="multipart/form-data">           
    <div class="row clearfix">
        <div class="col-md-6 bottommargin-sm photograph_part">
            <label>Upload Your Photograph</label><br>
            <input id="photograph" name="photograph" type="file" class="file">
        </div>
    </div>
    <button class='button button-rounded nomargin' id='scfbutton4' value='next'>Submit</button>
</form>

阿贾克斯:

$('#scfbutton4').click(function(e){
    $.post("<?php echo BASE_URL.'/startcf/save_form4';?>",  $('#svf-form-4').serialize(),function(data){
        console.log(data); //Output: {a: {error: "<p>You did not select a file to upload.</p>"}}
    }
    e.preventDefault();
});

控制器

public function save_form4(){
    $config['upload_path']          = './uploads/photograph';
    $config['allowed_types']        = 'jpg|png|pdf';
    $config['max_size']             = 1024;
    $this->load->library('upload', $config);
    if($this->upload->do_upload('photograph'))
    {
        //$data = array('upload_data' => $this->upload->data());
        header('Content-Type: application/json');
        echo json_encode( array('a' => 1) );
        return;     
    }
    else{
        $error = array('error' => $this->upload->display_errors());
        header('Content-Type: application/json');
        echo json_encode( array('a' => $error) );
        return;
    }
}

有人可以帮忙找出我做错了什么吗?

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    试试这个代码:

    序列化在上传文件时不起作用,请尝试以下方法:

    <script type="text/javascript">
    function go_form(){
    $.ajax({
     type:'POST',
     url:'testing2.php',     
     data:new FormData($('#svf-form-4')[0]),
     cache: false,
     contentType: false,
     processData: false,
     success:function(msg){
    
         $('#message').html(msg);
     }
    });
    return false;
     }
    </script>
    

    在表单上提交这个调用方法。

    【讨论】:

    • 你不能只介绍FormData 之类的东西而不解释它是什么,它做了什么。以及为什么 OP 应该使用它。
    • 反正@Shanil Soni 的@Magnus Eriksson 问题已经解决了……哈哈
    • @HarshPanchal 很好,我真的得到了帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多