简体   繁体   English

AJAX文件上传以将数据放入数据中

[英]AJAX file upload to put data into data

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: 我如何在AJAX请求的data:部分中放置另一个数据,例如:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM