文件上传功能实现

文件上传功能实现_第1张图片
效果图.png
文件名
文件大小
文件描述

var htmlModalFile = [
    '',
].join('');

$(function() {
    var modalFile = $(htmlModalFile);

    $('body').append(modalFile);

    $("#file_name").click(function () {
        $("#modal-upload").modal("toggle");
    });
    $("#btn_upload", modalFile).on("click", function () {
        var formData = new FormData($("#form-file")[0]);
        $.ajax({
            url: '/admin/file/upload',
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (data) {
                var par1 = window.document.getElementById('file_name');
                $(par1).attr('value', data['name']);
                var par2 = window.document.getElementById('file_size');
                $(par2).attr('value', data['size']);
                var save_type = $('#save_type option:selected') .val();
                $("#file_path").attr('value' ,save_type);
                alert(data['msg']);
            },
            error: function (data) {
                alert('ajax请求失败');
            }
        });
        return false;
    });
});
 /*
     * 保存文件基本信息
     */
    public function editAct(Request $request)
    {
        $data = $this->getParams(array('uid', 'file_name', 'description', 'file_size', 'file_path'));
        $data['edit_time'] = time();
        $data['app_name'] = get_app_name();
        if (!empty($data['uid'])) {
            $origin_data = Db::table('h_file')->where(['uid' => $data['uid']])->find();
            if ($origin_data['file_name'] != $data['file_name']) {
                $origin_name = get_down_path($origin_data['file_name'], $origin_data['file_path']);
                $new_name = get_down_path($data['file_name'], $data['file_path']);
                rename($origin_name, $new_name);
            }
        }
        if (!empty($data['uid'])) {
            Db::table('h_file')->where('uid', $data['uid'])->update($data);
            return $this->success('文件编辑成功!', url('file/edit', ['uid' => $data['uid']]));
        } else {
            $data['uid'] = create_uuid();
            Db::table('h_file')->insert($data);
            return $this->success('文件添加成功!', url('file/index'));
        }
    }

    public function upload(Request $request)
    {
        // 获取表单上传文件
        $file = $request->file('file');
        $save_type = $request->param('save_type');
        if (empty($file)) {
            return json(array('code' => -1, 'msg' => '请选择上传文件'));
        }
        $name = $file->getInfo('name');
        $file_path = get_down_path($save_type, '');
        if (file_exists(join_url($file_path, $name))) {
            return json(array('code' => -3, 'msg' => '文件已经存在','name' => $file->getInfo('name'),'size' => $file->getInfo('size')));
        }
        $info = $file->move($file_path, '');
        if ($info) {
            $file_info = $info->getInfo();
            return json(array('code' => 0, 'msg' => '文件上传成功', 'name' => $file_info['name'], 'size' => $file_info['size']));
        } else {
            // 上传失败获取错误信息`
            return json(array('code' => -2, 'msg' => '文件上传失败'));
        }
    }

你可能感兴趣的:(文件上传功能实现)