• 网页上查看上传图片


    1.在form中,必须有 enctype 提交方法必须是POST

     <form class="formAjax" enctype="multipart/form-data" method="POST" id="modelform">

    2.上传图片,可以在网页中查看

                    <tr rowspan='4'>
                        <?php if ($url_method =='create'): ?>
                            <img src="/images/photo-empty.jpg" alt=""
                                 style="110px;height:139px;float:left;padding-left:210px;" id="photopath">
                        <?php else: ?>
                            <img src="" alt=""
                                 style="110px;height:139px;float:left;padding-left:210px;" id="photopath">
                        <?php endif; ?>
                    </tr>
                    <tr>
                        <p class="short-input ue-clear">
                            <input type="file" name="addfile" id="proimg" style="position: relative;left: 145px;"
                           onchange="Preview(this,'photopath','divphotopath');">
                        </p>
                    </tr>

         js代码

     function Preview(obj, obj1, obj2) {
            if (document.all) {
                $("#" + obj1).css("width", "0px");
                $("#" + obj1).css("height", "0px");
                $("#" + obj2).css("display", "block");
                document.getElementById('' + obj2 + '').filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = obj.value;
            } else {
                $('#' + obj1).show();
                var file = obj, objectURL = window.URL.createObjectURL(file.files[0]);
                $('#' + obj1).attr("src", objectURL);
            }
        }

    3.不能用ajax传递,必须用ajaxSubmit进行传递

     $("#modelform").ajaxSubmit({
    })

     4.php后端代码

                $config['upload_path'] = FCPATH . 'uploads/userphoto/';
                $config['allowed_types'] = 'png|jpg|jpeg';
                $config['max_size'] = '2000000';
                $config['overwrite'] = FALSE;
                $config['encrypt_name'] = TRUE;
                $CI =& get_instance();
                $CI->load->library('upload', $config);
                if ($_FILES['addfile']['name'] != '') {
                    if ($CI->upload->do_upload('addfile')) {
                        $str = $CI->upload->data();
                        $file = realpath(FCPATH . 'uploads/userphoto/');
                        if (!is_dir($file)) mkdir($file);
                        $filename = $datainfo[$this->mydb->getKeyId()] . date("YmdHis") . $str['file_ext'];
                        $file = $file . '/' . $filename;
                        if (is_file($file)) unlink($file);
                        copy($str['full_path'], $file);
                        unlink($str['full_path']);
                        $file = "http://" . $_SERVER["HTTP_HOST"] . '/uploads/userphoto/' . $filename;
                        $datainfo ['photopath'] = $file;
                    } else {
                        $result = Ousuclass::getJsonResult();
                        $result['success'] = FALSE;
                        $result['msg'] = '上传文件错误!';
                        echo json_encode($result);
                        exit;
                    }
                }
  • 相关阅读:
    Tensorflow基础教程11:常用模块 tf.data :数据集的构建与预处理
    Tensorflow基础教程10:常用模块 TensorBoard:训练过程可视化
    Tensorflow基础教程9:常用模块 tf.train.Checkpoint 之变量的保存与恢复
    Tensorflow基础教程8:自定义层、损失函数和评估指标
    OC原理之多线程中的锁(一)
    OC原理之多线程(二)
    OC原理之多线程(一)
    OC原理之RunLoop的运行逻辑
    OC原理RunLoop(一)
    前端模块化:CommonJS,AMD,CMD,ES6
  • 原文地址:https://www.cnblogs.com/lqw4/p/4929528.html
Copyright © 2020-2023  润新知