The gin framework of go implements the interface that accepts multiple pictures and a single video and saves them to the local server

The first is the interface that accepts multiple pictures, that is, accepts multiple files

After receiving the post request, first create a folder, here use uuid to create a unique identification string as the folder name, parse a string of files in the form and save them to the local server in a loop

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"net/http"
)

func saveImages() {
	router := gin.Default()
	router.POST("", func(context *gin.Context) {
		form, err := context.MultipartForm()
		if err != nil {
			context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
		folder := uuid.New().String()
		for _, file := range form.File["file"] {
			err := context.SaveUploadedFile(file, "emergency/images/"+folder+"/"+file.Filename) //视频存储服务器的地址
			if err != nil {
				context.JSON(http.StatusInternalServerError, err.Error())
				return
			}
		}
		context.String(http.StatusOK, folder)
	})
	err := router.Run(":8080")
	if err != nil {
		println(err.Error())
		return
	}
}

For a single video file, it is of course possible to use the above code, but for a single file, if the request contains only one file, we do not need to use multipartform

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"net/http"
)

func saveVideo() {
	router := gin.Default()
	router.POST("", func(context *gin.Context) {
		file, err := context.FormFile("file")
		if err != nil {
			context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
		folder := uuid.New().String()
		err = context.SaveUploadedFile(file, "emergency/video/"+folder+"/"+file.Filename) //视频存储服务器的地址
		if err != nil {
			context.JSON(http.StatusInternalServerError, err.Error())
			return
		}
		context.String(http.StatusOK, folder)
	})
	err := router.Run(":8080")
	if err != nil {
		println(err.Error())
		return
	}
}

Supongo que te gusta

Origin blog.csdn.net/weixin_62264287/article/details/132650342
Recomendado
Clasificación