• beego 点滴


    在使用beego时遇到 need a primary key field

    1 确保结构中的 字段首字母大写
    2 beego默认主键是id  如果主键定义的是其他字段比如userid 那么加上orm pk
    
    type User struct {
        Id int  `orm:"pk;column(Userid);"`
        Name string
    }

     beego 上传文件

    package main
    
    import (
        "github.com/astaxie/beego"
    )
    
    type UploadController struct {
        beego.Controller
    }
    func (this *UploadController) Post() {
        this.SaveToFile("uploadfile", "./static/files/",true)
        this.Ctx.Redirect(302, "/upload")
    
    }
    /*
     * fromfile  input type=file name=fromfile
     * tofilepath the path where the file going to save
     * newfilename boolen  is create new name or use original name
     */
    func (c *UploadController) SaveToFile(fromfile, tofilepath string, newfilename bool) error {
        file, h, err := c.Ctx.Request.FormFile(fromfile)
        if err != nil {
            return err
        }
        defer file.Close()
        newname := h.Filename
        if newfilename {
            newname = fmt.Sprintf("%v%s", time.Now().Unix(), path.Ext(h.Filename))
        }
        tofilepath = path.Join(tofilepath, newname)
        f, err := os.OpenFile(tofilepath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
        if err != nil {
            return err
        }
        defer f.Close()
        io.Copy(f, file)
        return nil
    }
    func main() { 
      beego.Router(
    "/upload", &UploadController{}) beego.Run()
    }
    <head>
        <title>上传文件</title>
    </head>
    <body>
    
    <form enctype="multipart/form-data" action="/upload" method="post">
      <input type="file" name="uploadfile" />
      <input type="submit" value="upload" />
    </form>
    
    </body>
    </html>
  • 相关阅读:
    浅谈servlet源码
    移动端测试的8大过程
    Pc端微信加群和app端加群测试用例设计点
    视频播放的用例设计点
    发红包的测试用例设计点
    朋友圈点赞用例设计点
    ADB
    语音 小说 逻辑
    nginx
    Jenkins—安装与简单使用
  • 原文地址:https://www.cnblogs.com/wangxusummer/p/4413866.html
Copyright © 2020-2023  润新知