AJAX file upload to put data into data

kathleen55

I have this script that will upload multiple files and it will be retrieve by the controller.

Question

How can I put another data in the data: section in the AJAX request for example like this:

data: data + '&reference='+$('#ref').val(),

controller

function insertAttachment() {
    $i = 0;
    $referenceNo = $this->input->post('reference');
    
    if(!isset($_FILES[$i]) ) {
    
    }
    
    else {
        $x = $_FILES[$i]['name'];
        $xx = explode('.', $x);

        $config['upload_path'] = 'MRS-files\Upload_files';
        $config['allowed_types'] = 'xls|doc|jpg|png|gif|pdf';
        $this->load->library('upload',$config);

        for($i; $i <= 4; $i++) {
            $counter = $_FILES;

            while ( $i <= count($counter) ) {
                $x = $_FILES[$i]['name'];
                $xx=explode(".", $x);

                $config['file_name']= 'IT2015' .'('. ($i+1) .').'. $xx[1];
                $this->upload->initialize($config);

                $_FILES['up']['name']       = $_FILES[$i]['name'];
                $_FILES['up']['tmp_name']   = $_FILES[$i]['tmp_name'];
                $_FILES['up']['type']       = $_FILES[$i]['type'];
                $_FILES['up']['size']       = $_FILES[$i]['size'];

                if ( ! $this->upload->do_upload('up')) {
                    //error on uploading
                    echo str_replace('','',$this->upload->display_errors()); //temporary commented no use cause of redirect to homepage
                    //$this->cancelREC();
                    exit();
                }

                else {
                    $data = array('upload_data' => $this->upload->data());
                    $this->edit_development_model->insertonAttachments($data['upload_data'] , $referenceNo);
                    $i++;
                }
            }
        }
    }
}

Here is the script:

function EditUploadImage() {
    var data = new FormData($('input[name^="edit_files"]'));
    
    jQuery.each($('input[name^="edit_files"]')[0].files, function(i, file) {
        data.append(i, file);
    });

    $.ajax ({
        type: 'POST',
        data: data,
        url: 'mis.php/edit_development_detailsControl/updateRequest',
        cache: false,
        contentType: false,
        processData: false,
        success: function(data) {
            alert(data);
            //messagealert("Success","You're Request have been save successfully");
        }
    });
}
user4707168

Hope this one help you.

var fd = new FormData();
var file_data = $('input[type="file"]')[0].files; // for multiple files
for(var i = 0;i<file_data.length;i++){
    fd.append("file_"+i, file_data[i]);
}
var other_data = $('form').serializeArray();
$.each(other_data,function(key,input){
    fd.append(input.name,input.value);
});
$.ajax({
    url: 'test.php',
    data: fd,
    contentType: false,
    processData: false,
    type: 'POST',
    success: function(data){
        console.log(data);
    }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AJAX post multiple data after file upload

From Java

AJAX target file wont show data for upload processing

From Dev

JQuery ajax file upload to ASP.NET with all form data

From Dev

jQuery / AJAX - send additional data together with file upload

From Dev

upload file via Ajax appending data with append() not working

From Dev

file and data upload using ajax with spring mvc, jsp

From Dev

Retrieve file upload data

From Dev

Upload File - Multiform Data

From Dev

Upload JSON data as a file

From Dev

put data into CKEditor field with ajax

From Dev

Wicket: upload data via ajax

From Dev

Put array data on a csv file

From Dev

send File and Data in Ajax

From Dev

Upload file binary data into variable

From Dev

File Upload with spring data - gridfs

From Dev

Adding new data with a file upload

From Dev

Ng file upload with another data

From Dev

Put Json/Jsonp data to a form using ajax

From Dev

Jquery.ajax PUT data not parsed by PHP

From Dev

JQuery Ajax PUT request not sending data to the server

From Dev

jquery upload and post data throught ajax

From Dev

Multiple Ajax Form Data Upload Jquery

From Dev

ajax upload image with another data in form

From Dev

PHP file upload: Read raw file data

From Dev

Rails - form doesn't submit any data during AJAX call - file upload - 422 status code

From Dev

Unable to print success messages after data is successful file upload in jquery ajax

From Dev

Why ajax upload file doesn't need enctype="multipart/form-data" in the form tag?

From Dev

QT upload (PUT) a file

From Dev

QT upload (PUT) a file