0

I'm getting an error 'you did not select a file to upload' in codeigniter. Don't what I'm doing wrong, here is my code.

View

<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>

Ajax:

$('#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();
});

Controller

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;
    }
}

Can somebody help in finding what I'm doing wrong?

Guy in the chair
  • 1,045
  • 1
  • 13
  • 42

1 Answers1

2

Try This code :

Serialize is not working when upload file so try this method below:

<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>

On Form submit this call method.

Harsh Panchal
  • 308
  • 1
  • 8