Golang上传静态文件

Golang上传静态文件

1.学习链接

学习链接 – GoFrame v2.0 开发文档 – 静态文件服务

2.开发步骤

① config.yaml配置静态文件上传路径

# 文件上传配置
upload:
  path: "upload"

② 上传单个文件代码

❤ 参数结构体

package model

import "github.com/gogf/gf/v2/net/ghttp"

// FileUploadInput 上传文件输入参数
type FileUploadInput struct {
	File       *ghttp.UploadFile // 上传文件对象
	Name       string            // 自定义文件名称
	RandomName bool              // 是否随机命名文件
}

// FileUploadOutput 上传文件返回参数
type FileUploadOutput struct {
	Name string // 文件名称
	Path string // 本地路径
	Url  string // 访问URL,可能只是URI
}

❤ 上传业务逻辑

package file

import (
	"context"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gfile"
	"github.com/gogf/gf/v2/os/gtime"
	"jw-back/internal/model"
	"jw-back/internal/service"
)

type sFile struct{}

func init(){
	service.RegisterFile(New())
}

func New()  *sFile{
	return &sFile{}
}

//上传文件
func (s *sFile) Upload(ctx context.Context,in model.FileUploadInput) (*model.FileUploadOutput,error) {
	uploadPath := g.Cfg().MustGet(ctx,"upload.path").String()
	if uploadPath == "" {
		return nil, gerror.New("上传文件路径配置不存在")
	}
	if in.Name != "" {
		in.File.Filename = in.Name
	}
	dateDirName := gtime.Now().Format("Ymd") //年月日
	fileName, err := in.File.Save(gfile.Join(uploadPath, dateDirName),in.RandomName)
	if err != nil {
		return nil, err
	}
	return &model.FileUploadOutput{
		Name: fileName,
		Path: gfile.Join(uploadPath, dateDirName, fileName), // upload/20230302/cqfgfefdf.png
		Url:  "/upload/" + dateDirName + "/" + fileName,
	}, nil
}

③ cmd.go文件设置静态文件目录

var (
	Main = gcmd.Command{
		Name:  "main",
		Usage: "main",
		Brief: "start http server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			var (
				s = g.Server()
			)

			// 静态目录设置 -- 添加URI与静态目录的映射
			uploadPath := g.Cfg().MustGet(ctx,"upload.path").String()
			if uploadPath == "" {
				g.Log().Fatal(ctx, "文件上传配置路径不能为空")
			}
			s.AddStaticPath("/upload",uploadPath)  //ip:端口/test = ip:端口/upload

			// HOOK, 开发阶段禁止浏览器缓存,方便调试
			if gmode.IsDevelop() {
				s.BindHookHandler("/*",ghttp.HookBeforeServe,func(r *ghttp.Request) {
					r.Response.Header().Set("Cache-Control", "no-store")
				})
			}
			
			s.Group("/", func(group *ghttp.RouterGroup) {
				group.Middleware(ghttp.MiddlewareHandlerResponse) // 处理处理 程序响应对象 及其 错误 的默认中间件。
				group.Bind(
					controller.File,
				)
			})

			// 启动Http Server
			s.Run()
			return nil
		},
	}
)

④ 控制层调用上传业务逻辑

❤ 请求结构体

package v1

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
)

type FileUploadReq struct {
	g.Meta `path:"/file" method:"post" mime:"multipart/form-data" tags:"工具" summary:"上传文件"`
	File *ghttp.UploadFile `json:"file" type:"file" dc:"选择上传文件"`
}

type FileUploadRes struct {
	Name string `json:"name" dc:"文件名称"`
	Url  string `json:"url"  dc:"访问URL,可能只是URI"`
}

❤ Controller层调用上传业务逻辑

package controller

import (
	"context"
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	v1 "jw-back/api/v1"
	"jw-back/internal/model"
	"jw-back/internal/service"
)

var File = cFile{}

type cFile struct{}

//上传文件
func (c *cFile) Upload(ctx context.Context, req *v1.FileUploadReq) (res *v1.FileUploadRes, err error) {
	if req.File == nil {
		return nil, gerror.NewCode(gcode.CodeMissingParameter, "请选择需要上传的文件")
	}
	result, err := service.File().Upload(ctx, model.FileUploadInput{
		File:       req.File,
		RandomName: true,
	})
	if err != nil {
		return nil , err
	}
	res = &v1.FileUploadRes{
		Name: result.Name,
		Url: result.Url,
	}
	return

⑤ Postman 测试接口

Golang上传静态文件_第1张图片

我的端口号是8002,访问链接: http://127.0.0.1:8002/file
请求:post
请求体:form-data

⑥ 文件显示位置

Golang上传静态文件_第2张图片

你可能感兴趣的:(GoFrame,golang)