• Gin框架之文件上传


    一、单文件上传

    前端代码

     1 <!DOCTYPE html>
     2 <html lang="zh-CN">
     3 <head>
     4     <title>上传文件示例</title>
     5 </head>
     6 <body>
     7 <form action="/uploadfiles" method="post" enctype="multipart/form-data">
     8     <input type="file" name="f1">
     9     <input type="submit" value="上传">
    10 </form>
    11 </body>
    12 </html>

    后端代码

     1 func main() {
     2     //创建默认的引擎
     3     r := gin.Default()
     4     //告诉gin框架去哪加载讲台文件 此处可以使用正则表达式
     5     r.LoadHTMLGlob("views/index.html")
     6     //get请求返回显示页面 index.html
     7     r.GET("/index", func(c *gin.Context) {
     8         c.HTML(http.StatusOK, "index.html", nil)
     9     })
    10     //创建请求 当访问地址为/uploadfile时执行后面的函数
    11     r.POST("/uploadfile", func(c *gin.Context) {
    12         //获取表单数据 参数为name值
    13         f, err := c.FormFile("f1")
    14         //错误处理
    15         if err != nil {
    16             c.JSON(http.StatusBadRequest, gin.H{
    17                 "error": err,
    18             })
    19             return
    20         } else {
    21             //将文件保存至本项目根目录中
    22             c.SaveUploadedFile(f, f.Filename)
    23             //保存成功返回正确的Json数据
    24             c.JSON(http.StatusOK, gin.H{
    25                 "message": "OK",
    26             })
    27         }
    28     
    29     })
    30     //运行 默认为80端口
    31     r.Run()
    32 }

    二、多文件上传

    前端代码

     1 <!DOCTYPE html>
     2 <html lang="zh-CN">
     3 <head>
     4     <title>上传文件示例</title>
     5 </head>
     6 <body>
     7 <form action="/uploadfiles" method="post" enctype="multipart/form-data">
     8     <input type="file" name="f1s">
     9     <input type="file" name="f1s">
    10     <input type="submit" value="上传">
    11 </form>
    12 </body>
    13 </html>

    后端代码

     1 //多文件上传
     2     r:=gin.Default()
     3     r.LoadHTMLGlob("views/index.html")
     4     r.MaxMultipartMemory = 8 << 20  // 8 MiB 设置最大的上传文件的大小
     5     r.GET("/index", func(c *gin.Context) {
     6         c.HTML(http.StatusOK,"index.html",nil)
     7     })
     8     r.POST("/uploadfiles", func(c *gin.Context) {
     9         form,err:=c.MultipartForm()
    10         files:=form.File["f1s"]
    11         //错误处理
    12         if err != nil {
    13             c.JSON(http.StatusBadRequest, gin.H{
    14                 "error": err,
    15             })
    16             return
    17         }
    18         for _,f:=range files{
    19             fmt.Println(f.Filename)
    20             c.SaveUploadedFile(f,f.Filename)
    21         }
    22         c.JSON(http.StatusOK, gin.H{
    23             "message": "OK",
    24         })
    25     })
    26     r.Run()

    运行:go run main.go

    访问地址:http://127.0.0.1:8080/index

  • 相关阅读:
    Tomcat建立多个应用(Web Server),多个主机,多个站点的方法
    Spring 注解bean默认名称规则
    spring+springMVC,声明式事务失效,原因以及解决办法
    Spring事务失效的原因
    MySQL 对于千万级的大表要怎么优化?
    前端开发利器: Bootstrap + AngularJS
    scrapy爬取段子
    scrapy安装
    xpath和CSS选择器
    pycharm远程登录mysql
  • 原文地址:https://www.cnblogs.com/yh2924/p/12377309.html
Copyright © 2020-2023  润新知