2

I have created a script to upload the image with text but the problem is that when we post the data and upload the image the file is coming empty I do not know why the file is coming empty can anyone help me out to get the correct file

This is my ajax script

$(document).ready(function(e) {
$("#post").on('submit', (function(e) {
    $("#load").show();

    var form = this;
    var formData = new FormData(this);
    alert(formData);
    formData.append('post_dt', $("#contentbox").html());

    $.ajax({
        url         : "http://domainname.com/demo/forums/post_forum",
        type        : "POST",
        data        : formData,
        contentType : false,
        cache       : false,
        processData : false,
        success     : function(data) {
            $("#data_update").prepend($(data).fadeIn('slow'));
            $("#contentbox").empty();
            form.reset();
        },
        complete: function() {
            $("#load").hide();
        }
    });
}));
});

This the html form which is being created

<form method="POST" action="" id="post" enctype="multipart/form-data" onsubmit="return false;">
                <ul>
                    <li>
                        <i class="fa fa-photo"></i> Upload A Photo / Document
                        <input type="file" name="image" />
                    </li>
                </ul>
                <div id='display'></div>
                <div id="contentbox" contenteditable="true" name="post_dt">
                </div>

                <input type="submit" id="sb_art" class="btn_v2" value="Start Discussion" />
            </form>

My php script which I have created within the controller

public function post_forum() {
    $config['upload_path']   = './assets/uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = 3000;
    $this->load->library('upload', $config);

    if(!empty($this->input->post('image'))) {
        if (!$this->upload->do_upload('image')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $dt = $this->upload->data();
            $file = $dt['file_name'];
        }
    } else {
        $file = 'Filename.jpg';
    }
    $word_dta    = $this->input->post('post_dt');
    $time_posted = time();
    $user_id     = $this->basic_model->is_login($this);

    $data_upload = array(
        'upload_document' => $file,
        'discussion'      => $word_dta,
        'user_id'         => $user_id,
        'time_posted'     => $time_posted,
        'status'          => 'Posted'
    );

    $post_id = $this->basic_model->insertRecord($data_upload, 'forums');

    $data = array(
        'file_name'   => $file,
        'data'        => $word_dta,
        'time_posted' => $time_posted,
        'post_id'     => $post_id,
        'name'        => $this->basic_model->getUserData(),
        'command'     => 'Submit Post'
    );

    $this->load->view('forum/includes/get_data', $data);
}
Mark Alan
  • 435
  • 1
  • 5
  • 19

3 Answers3

0

add id as imageFieldId to file in html. Then try

        var file_data = $('#imageFieldId').prop('files')[0];   
        var form_data = new FormData();                  
        form_data.append('file', file_data);
       //formData.append('post_dt', $("#contentbox").html());

    $.ajax({.....
safin chacko
  • 1,345
  • 1
  • 11
  • 18
0

I have sorted out my issue I was making a mistake within the controller which was my mistak so I have resolved my issues take care

Mark Alan
  • 435
  • 1
  • 5
  • 19
-1

Ensure your webserver (nginx/apache) upload size limit are correct.

This link could be your reference to set the upload size:

Increase File Upload Size Limit

Edit NginX Conf to Increase File Size Upload

Community
  • 1
  • 1
steven
  • 104
  • 4
  • Why the down-vote? I'm giving you another aspect to check of...Because you didn't mention anything about it in your question. I wonder if in the future I will be willingly answer your question. – steven Mar 13 '17 at 11:51
  • I did not down voted you and my question is clarified what else I can explain is anything I am missing? – Mark Alan Mar 13 '17 at 11:56