dougaimian1143 2015-03-25 13:08
浏览 40
已采纳

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

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");
        }
    });
}
  • 写回答

1条回答 默认 最新

  • doupeizheng3918 2015-03-25 14:15
    关注

    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);
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?