700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > tp6腾讯云 七牛云对象存储的工具类以及异步上传

tp6腾讯云 七牛云对象存储的工具类以及异步上传

时间:2019-06-12 10:06:09

相关推荐

tp6腾讯云 七牛云对象存储的工具类以及异步上传

composer安装

composer require lorine/oss-utils

代码:

use Lorine\OssUtils\OssService;$config = ['ak' => 'xxxxxx',//SecretId /Access_Key'sk' => 'xxxxxx',//SecretKe /Secret_Key'bucket' => 'xxxxx',//桶名'region' => ''//地区 七牛云为'',腾讯云在控制台对象存储界面获取,如上海(ap-shanghai)];try {//云存储类型 腾讯云:Tencent 七牛云:Qiniu$obj = (new OssService())->getOssService('Tencent');//$tmpName绝对路径$tmpName=$request->file('image');//$fileName自定义云储存的文件名称$fileName=md5(date('Ymd His'));$res = $obj->uploadFile($config,$tmpName,$fileName);dd($res);}catch (Exception $exception){dd($exception->getMessage());}

异步上传:

前端

<form action="{:url('/img')}" id="upForm" method="post" enctype="multipart/form-data"><input type="file" name="img" id="upload"><img src="" alt="" class="img"><input type="submit" value="提交"></form>

JqueryAJAX

<script src="/libs/jquery/2.1.4/jquery.min.js"></script><script>$('#upload').change(function (){$.ajax({url:"img",type: "POST",//方法类型cache : false,//processData: false,contentType: false,dataType:"json",//data:$('#upForm').serialize(),data: new FormData($('#upForm')[0]),beforeSend: function(){uploading = true;console.log(uploading);},success: function(data){console.log(data.path);$('.img').attr('src',data.path);}});})</script>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。