首页 > 代码库 > 通过ifreme实现文件上传

通过ifreme实现文件上传

模板页面添加ifreme
<div style=‘ display: none;‘ >
     <iframe name ="uploadResponse_attachment" id= "uploadResponse_attachment"></iframe >
    <form id ="uploadForm_attachment" action= "<?php echo site_url(‘logistics/upload_chartering_attachment‘) ?>" target= "uploadResponse_attachment" method ="post" enctype= "multipart/form-data">
     <input type ="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value= "<?php echo $this->security->get_csrf_hash(); ?>" />
    </form >
</div>
 
对应js页面添加
//上传租船附件
function upload_attachment_chartering()
{
     $(‘#attachment_img_notice‘).show();
    var file = document.getElementById( "chartering_attachment");
    var uploadFormElement = document.getElementById("uploadForm_attachment" );
   
    uploadFormElement .appendChild(file);
    document.getElementById("attachment_img_div").innerHTML = ‘<a href="javascript:void(0)" class="btn_blue_26"><span>上传</span></a><input type="file" onchange="upload_attachment_chartering()" name="chartering_attachment" multiple="multiple" id="chartering_attachment">‘;
 
    //提交图片数据
    uploadFormElement .submit();
    uploadFormElement.removeChild( uploadFormElement.chartering_attachment);
}
 
//上传成功回掉
function upload_attachment_chartering_callback(file_path)
{
    $(".attachment_show_img").attr( ‘src‘,file_path);
    $(‘#attachment_image‘).val(file_path);
    $(‘#attachment_img_notice‘).hide();
    $(‘.attachment_error‘).html( ‘‘);
    $(‘.attachment_error‘).hide();
}
 
//上传失败回掉
function upload_attachment_chartering_fail_callback(fail_remind)
{
     $(".attachment_show_img").attr( ‘src‘, ‘/images/11.gif‘);
     $(‘#attachment_image‘).val( ‘‘);
     $(‘#attachment_img_notice‘).show();
    $(‘.attachment_error‘).html(fail_remind);
    $(‘.attachment_error‘).show();
}
 
//控制器
public function _upload_chartering_attachment_post()
     {
           $data = http://www.mamicode.com/upload_member_voucher(‘chartering_attachment‘ );
            if($data[ ‘succ‘] == 1){
                $file_path = $data[ ‘file_path‘];
                 echo "<script type=‘text/javascript‘>parent.upload_attachment_chartering_callback(‘$file_path ‘)</script>";
           } else{
                $fail_reason = $data[ ‘fail_reason‘];
                 echo "<script type=‘text/javascript‘>parent.upload_attachment_chartering_fail_callback(‘".$fail_reason. "‘)</script>";
           }
     }
 
//公用上传方法
function upload_member_voucher($form_file_id = ‘‘ ) {
            if (!$form_file_id) {
                 return;
           }
           $ci = &get_instance();
           $cert_dir = $ci-> config->item( ‘cert_path‘) . ‘/‘;
           $day_dir = date( ‘Ym‘);
           $physics_path = FCPATH . ‘..‘ . $cert_dir . $day_dir;
            if (!is_dir($physics_path)) {
                mkdir($physics_path, 0755);
           }
           $config[ ‘upload_path‘] = $physics_path; //文件上传目录
 
           $config[ ‘encrypt_name‘] = true;
           $config[ ‘allowed_types‘] = "gif|jpg|png|tif"; //文件类型
           $config[ ‘max_size‘] = "1048"; //最大上传大小
           $ci-> load->library( "upload", $config);
 
            if ($ci-> upload->do_upload($form_file_id)) { //表单中name="userfile"
                $data = http://www.mamicode.com/$ci-> upload->data(); //返回上传图片的信息
                $res[ ‘succ‘] = 1;
                $res[ ‘file_path‘] = $cert_dir . $day_dir . ‘/‘ . $data[‘file_name‘];
           } else{
                $res[ ‘succ‘] = 0;
                $res[ ‘fail_reason‘] = $ci->upload->display_errors();
           }
            return $res;
     }

通过ifreme实现文件上传