当前位置:网站首页>[go language development] start to develop Meitu station from 0 - Lesson 5 [receive pictures and upload]

[go language development] start to develop Meitu station from 0 - Lesson 5 [receive pictures and upload]

2022-06-24 17:15:00 User 2353021

From the beginning of this lesson, we have entered a more difficult step . This step is the core of our service , Receive picture upload . Before writing this , Let's start with another piece of software , The name of this software is postman

[ Download address ]:Postman

Postman This is for debugging interfaces . First download postman Click the address to see the following page .

Click on Download the App Then select 64-bit Download the version of .

Double click to install Postman Once installed , If you have an account , You can click login , If not , You can click on the Create Free Account Create , That is, email, password and password confirmation .

If you don't want to create an account , You can also directly click the first face Skip and go to the app Start trial directly .

The interface is as follows .

Here we are familiar with Postman The next step is to start writing code .

package main

import (
	"fmt"
	"log"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	fmt.Println(" Hello world ")
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})

	//  Start uploading interface 
	r.POST("/upload", func(c *gin.Context) {
		//  obtain Formdata Upload file object 
		file, err := c.FormFile("file")
		//  Determine whether there is an error return 
		if err != nil {
			//  If there is a record error, it returns 
			log.Printf(" Error receiving file ,%v", err)
			//  And return the result directly 
			c.JSON(200, gin.H{
				"status": false,
				"code":   -1,
				"msg":    " Failed to upload file ",
			})
		} else {
			//  If not, get the file name 
			fileName := file.Filename
			//  Organize directories and file names , Form a save path 
			savePath := "./" + fileName
			//  Use gin Its own storage method , Save the file in the save Directory .
			c.SaveUploadedFile(file, savePath)
			c.JSON(200, gin.H{
				"status": true,
				"code":   200,
				"msg":    " Successful operation ",
			})
		}
	})
	//  End the interface for uploading files 

	r.Run(":8080")
}

You can see , I am here ping This interface ( An interface is defined as an access path , We can browse in the browser as well as Postman Address accessed in .) Another interface is written below , Namely upload Interface .

In this code , You can see many lines // Code at the beginning , And there is Chinese at the back . This is it. Golang The comments in . Comments serve to explain what the code means , But it does not participate in the actual operation of the program , You can understand it as the notes of the program .

Now? , Don't talk first , Copy the code above . Then press F5 Run up . Then sacrifice the one installed in front Postman To test whether our upload function is normal

According to the settings above , Then set it to the following , Then click the blue one on the right Send

Then you will find the following success marks .

And then you were visual Studio Code The uploaded file can be seen in the file management column on the left .

So far, the uploading code is completed

[go Language development ] from 0 Start to develop Meitu station —— The fourth lesson

[go Language development ] from 0 Start to develop Meitu station —— Lesson 6

No reprint without permission :RffanLAB|Rffan laboratory » [go Language development ] from 0 Start to develop Meitu station —— Lesson Five [ Receive picture upload ]

原网站

版权声明
本文为[User 2353021]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/03/20210329173203633D.html