Laravel 七牛云上传图片

Laravel七牛云上传图片DEMO

// html文件
@extends('layouts.admin')

@section('title', '上传文件')

@section('content')
        <!--我是主要内容 start-->
        <ul class="breadcrumb" style="font-size: 16px;">
            <li><a href="#">首页</a></li>
            <li><a href="{{ url('admin/files/index') }}">文件管理</a></li>
            <li class="active">上传文件</li>
        </ul>
        <div class="row">
            <div class="col-md-2 col-md-offset-9">
                <button type="button" class="btn btn-success" onclick="window.history.go(-1);">返回文件列表</button>
            </div>
        </div>
        <form class="form-horizontal">
            {{csrf_field()}}
            <div class="form-group">
                <label for="file_title" class="col-sm-2 control-label">文件标题</label>
                <div class="col-sm-6">
                    <input type="text" name="file_title" class="form-control" id="file_title" placeholder="请输入文件标题">
                </div>
            </div>
            <div class="form-group">
                <label for="file_path" class="col-sm-2 control-label">文件缩略图</label>
                <input class="form-conrol col-sm-6 uploadImg" type="file" id="file_path">
                <div class="col-sm-6" style="margin-top: 10px;">
                    <img src="" title="文件缩略图" width="100" class="img-rounded img-responsive file_path"/>
                    <input type="hidden" name="file_path" id="banner_img" value=""/>
                </div>
            </div>
            <div class="form-group">
                <label for="status" class="col-sm-2 control-label">状态</label>
                <label class="radio-inline">
                    <input type="radio" name="status" value="0"> 草稿
                </label>
                <label class="radio-inline">
                    <input type="radio" name="status" value="1" checked> 发布
                </label>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="button" class="btn btn-success btn-lg" onclick="submitBtn();"> 保 存</button>
                    <button type="button" class="btn btn-warning btn-lg" style="margin-left: 30px;" onclick="window.history.go(-1);"> 返 回</button>
                </div>
            </div>
        </form>
        <!--我是主要内容 end-->
@endsection
@section('my-js')
    <script>
        function submitBtn() {
            var file_title = $('input[name=file_title]').val();
            var file_path = $('input[name=file_path]').val();
            if(file_title === '') {
                layer.msg('banner标题不能为空');
                return;
            }
            if(file_path === '') {
                layer.msg('banner缩略图不能为空');
                return;
            }
            var data = $('.form-horizontal').serialize();
            $.ajax({
                type: "POST",
                url: "/admin/file/create",
                data: data,
                dataType: 'json',
                cache: false,
                success: function (data) {
                    if(data.code === 0) {
                        swal({
                            title: data.msg,
                            text: '',
                            type: 'success',
                            timer: 1000,
                            onOpen: () => {
                                swal.showLoading();
                            },
                            onClose: () => {
                                location.href = '/admin/banner/index';
                            }
                        })
                    }else{
                        layer.msg(data.msg);
                    }
                }
            });
        }

        // 上传图片
        $("#file_path").change(function () {
            layer.msg('文件上传中,请稍等...', {
                shade: [0.3]
            });
            var formData = new FormData();
            formData.append("file", $('#file_path')[0].files[0]);
            formData.append("_token", "{{csrf_token()}}");
            $.ajax({
                type: "POST",
                url: "/admin/file/upload",
                data: formData,
                dataType: 'json',
                processData: false,
                contentType: false,
                cache: false,
                success: function (data) {
                    if(data.code === 0) {
                        layer.msg('文件上传成功');
                        $('.file_path').attr('src', data.src);
                        $('#banner_img').val(data.data);
                    }
                }
            });
        });
    </script>

@endsection
// 服务器端
1.composer require itbdw/laravel-storage-qiniu
2.编辑 config/app.php 
providers 加上一行  itbdw\QiniuStorage\QiniuFilesystemServiceProvider::class,
3.编辑config/filesystems.php,添加
'qiniu' => [  
            'driver'    => 'qiniu',  
            'domain'    => 'xxxxx.bkt.clouddn.com',  //你的七牛域名  
            'access_key'=> '',    //AccessKey  
            'secret_key'=> '',   //SecretKey  
            'bucket'    => '',    //Bucket名字,即七牛云存储空间名称  
        ],  
4.上传方法
public function upload()
{
    $disk = \Storage::disk('qiniu'); //使用七牛云上传
    $time = date('Y-m-d');
    $filename = $disk->put($time, request()->file('file'));//上传
    if(!$filename) {
    return response()->json(['code' => 1, 'msg' => '上传失败']);
    }
    $img_url = $disk->getDriver()->downloadUrl($filename); //获取下载链接
    return response()->json(['code' => 0, 'msg' => '上传成功', 'src' => $img_url]);
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值