zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    SQL Server游标的使用【转】
    Window.Open参数、返回值
    .NET中class和struct的区别
    MVC中的几个问题汇总
    委托
    Hadoop集群环境搭建
    数学问题
    Ubuntu命令模式基础
    递归与分治-合并排序、快速排序以及循环赛问题
    Mongodb基础
  • 原文地址:https://www.cnblogs.com/wangxusummer/p/4413866.html
Copyright © 2011-2022 走看看