使用 golang的gin框架实现,文件上传

协变冰川
• 阅读 8414

一、引入 gin 框架

import "ginhub.com/gin-gonic/gin

二、渲染页面

router := gin.Default()
    router.LoadHTMLGlob("view/*")
    router.GET("/upload",func(c *gin.Context) {
        c.HTML(http.StatusOK,"upload.tmpl",gin.H{})
    })

三、处理上传文件

router.POST("/upload", func(c *gin.Context) {
        // Source
        file, err := c.FormFile("file")
        if err != nil {
            c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
            return
        }

        basePath := "./upload/"
        filename := basePath + filepath.Base(file.Filename)
        if err := c.SaveUploadedFile(file, filename); err != nil {
            c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
            return
        }

        c.String(http.StatusOK,fmt.Sprintf("文件 %s 上传成功 ", file.Filename))
    })

全部代码:

package main

import (
    "github.com/gin-gonic/gin"
    "net/http"
    "fmt"
    "path/filepath"
)

func main() {

    router := gin.Default()
    router.LoadHTMLGlob("view/*")
    router.GET("/upload",func(c *gin.Context) {
        c.HTML(http.StatusOK,"upload.tmpl",gin.H{})
    })


    router.MaxMultipartMemory = 8 << 20 // 8 MiB
    router.POST("/upload", func(c *gin.Context) {
        file, err := c.FormFile("file")
        if err != nil {
            c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
            return
        }

        basePath := "./upload/"
        filename := basePath + filepath.Base(file.Filename)
        if err := c.SaveUploadedFile(file, filename); err != nil {
            c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
            return
        }

        c.String(http.StatusOK,fmt.Sprintf("文件 %s 上传成功 ", file.Filename))
    })
    router.Run(":8080")
}

文档链接:https://github.com/gin-gonic/...

点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
码农印象 码农印象
5年前
【Golang】GoWeb框架之Gin-简明教程
Gin简介GinisaHTTPwebframeworkwritteninGo(Golang).Itfeaturesa
隔壁老王 隔壁老王
4年前
我的gin框架笔记
入门教程https://golangcaff.com/docs/gingonic/2018/ginreadme/3961.下载gogetugithub.com/gingonic/gin2.在你的代码中导入它:import"github.com/gingonic/gin"3.(可选的)导入net/http
Wesley13 Wesley13
4年前
go保存文件到指定的文件夹
框架:gin语言:golang作用:保存文件到本地指定文件夹的一个小demo单文件保存到本地指定目录:packagemainimport("fmt""log""net/http""path""github.com/gingon
Wesley13 Wesley13
4年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Stella981 Stella981
4年前
Golang 微框架 Gin 简介
所谓框架框架一直是敏捷开发中的利器,能让开发者很快的上手并做出应用,甚至有的时候,脱离了框架,一些开发者都不会写程序了。成长总不会一蹴而就,从写出程序获取成就感,再到精通框架,快速构造应用,当这些方面都得心应手的时候,可以尝试改造一些框架,或是自己创造一个。曾经我以为Python世界里的框架已经够多了,后来发现相比golang简直小巫见大巫。
Wesley13 Wesley13
4年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
4年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Wesley13 Wesley13
4年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0