注意:上传参数时可以配置的,在系统后台 >> 系统配置 >> 参数管理 进行附件类型,大小配置。 其中上传受http服务配置ClientMaxBodySize参数影响,客户端提交的Body大小限制,同时也影响文件上传大小,默认设置为8MB,若需要上传较大文件需要将此配置同时设置。

(捐赠版、授权版才有此功能)

上传配置

# 上传配置
upload:
  default: 0   # 默认上传至本地 0 - 本地 1 - 腾讯云 2 - 七牛云 3- 阿里oss
  tencentCOS:     # 腾讯云
    upPath : "/gfast/"
    rawUrl :    "https://xxx-xxxx.cos.ap-chongqing.myqcloud.com"
    secretID :  "xxxxxxxxxxxxxxxxxxxx"
    secretKey : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  qiniou: # 七牛云
    accessKey : "xxxxxxxxxxxxxxxxxxxxxx"
    sercetKey: "xxxxxxxxxxxxxxxxxxxxx"
    bucket : "gfast-test"
    qiniuServer : "http://xxxxxxxxxxx.hn-bkt.clouddn.com"
  oss: #阿里云oss
    accessKeyID: "xxxxxxxxxxxxxxxxxxx"
    accessKeySecret: "xxxxxxxxxxxxxxxxxxxx"
    endPoint: "xxx.xxxx.com" #自定域名
    bucketName: "xxx" #使用的存储桶
    isHttps: true #是否开启https访问
    path:"gfast" #上传文件保存路径

后台上传代码参考:internal/app/system/controller/upload.go

package controller

import (
    "context"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/tiger1103/gfast/v3/api/v1/system"
    "github.com/tiger1103/gfast/v3/internal/app/common/consts"
    "github.com/tiger1103/gfast/v3/internal/app/common/service"
)

var Upload = new(uploadController)

type uploadController struct{}

// 上传单图
func (c *uploadController) SingleImg(ctx context.Context, req *system.UploadSingleImgReq) (res *system.UploadSingleRes, err error) {
    file := req.File
    v, _ := g.Cfg().Get(ctx, "upload.default")
    response, err := service.Upload().UploadFile(ctx, file, consts.CheckFileTypeImg, v.Int())
    if err != nil {
        return
    }
    res = &system.UploadSingleRes{
        UploadResponse: response,
    }
    // 上传第三方
    return
}

// 上传多图
func (c *uploadController) MultipleImg(ctx context.Context, req *system.UploadMultipleImgReq) (res *system.UploadMultipleRes, err error) {
    files := req.File
    v, _ := g.Cfg().Get(ctx, "upload.default")
    mf, err := service.Upload().UploadFiles(ctx, files, consts.CheckFileTypeImg, v.Int())
    if err != nil {
        return
    }
    res = &mf
    return
}

// 上传单文件
func (c *uploadController) SingleFile(ctx context.Context, req *system.UploadSingleFileReq) (res *system.UploadSingleRes, err error) {
    file := req.File
    v, _ := g.Cfg().Get(ctx, "upload.default")
    response, err := service.Upload().UploadFile(ctx, file, consts.CheckFileTypeFile, v.Int())
    if err != nil {
        return
    }
    res = &system.UploadSingleRes{
        UploadResponse: response,
    }
    return
}

// 上传多文件
func (c *uploadController) MultipleFile(ctx context.Context, req *system.UploadMultipleFileReq) (res *system.UploadMultipleRes, err error) {
    files := req.File
    v, _ := g.Cfg().Get(ctx, "upload.default")
    mf, err := service.Upload().UploadFiles(ctx, files, consts.CheckFileTypeFile, v.Int())
    if err != nil {
        return
    }
    res = &mf
    return
}

前端已封装了相关组件:

作者:管理员  创建时间:2023-01-06 16:57
最后编辑:管理员  更新时间:2024-11-21 09:37