donglu1973 2015-08-04 04:42 采纳率: 100%
浏览 74
已采纳

gin-gonic中的分段文件上传验证

I am trying to add validation for the go based web application based on GIN framework. On the web page I am selecting a file and submitting and the server is processing it. On the server side I am try to add validation to check if the file has been given or not. If not then redirect back to original page.

    func panic(err error)  {
        if err != nil {
            log.Println(err)
        }
    }

    func displayTable (c *gin.Context) {    
    file, _ , err := c.Request.FormFile("file")
    panic(err)
    if file == nil {
        log.Println("File is nil.")
        log.Println(err)
        log.Println("*****")
        c.HTML(http.StatusInternalServerError, "index.tmpl", gin.H{
            "title": "Select the input file","error" : "Please select the input file.",
        })      
    } else {
        defer file.Close()
    }
    filename := strconv.FormatInt(time.Now().Unix(),10) 
    out, err := os.Create("./tmp/"+filename+".xml")
    panic(err)
    defer out.Close()
    _, err = io.Copy(out, file)
    panic(err)
    xmlFile, err := os.Open("./tmp/"+filename+".xml")
    panic(err)
    defer xmlFile.Close()

    // Other Implementation Details 
}

Even after providing the handling I am getting a panic in the go code. Please let me know what in the implementation am I missing.

Thanks.

    http: no such file
    File is nil.
    http: no such file
    *****
    2015/08/04 13:19:10 Panic recovery -> runtime error: invalid memory address or nil pointer dereference
    c:/go/src/runtime/panic.go:387 (0x414d36)
    c:/go/src/runtime/panic.go:42 (0x4142a5)
    c:/go/src/runtime/os_windows.go:42 (0x414066)
    c:/go/src/io/io.go:362 (0x45268f)
    D:/code/src/exmp/serverexmaple.go:45 (0x40168f)
            displayTable: _, err = io.Copy(out, file)
    D:/code/src/github.com/gin-gonic/gin/context.go:95 (0x49f8ea)
            (*Context).Next: c.handlers[c.index](c)
    D:/code/src/github.com/gin-gonic/gin/logger.go:56 (0x4ac490)
            func.007: c.Next()
    D:/code/src/github.com/gin-gonic/gin/context.go:95 (0x49f8ea)
            (*Context).Next: c.handlers[c.index](c)
    D:/code/src/github.com/gin-gonic/gin/recovery.go:43 (0x4acc80)
            func.009: c.Next()
    D:/code/src/github.com/gin-gonic/gin/context.go:95 (0x49f8ea)
            (*Context).Next: c.handlers[c.index](c)
    D:/code/src/github.com/gin-gonic/gin/gin.go:292 (0x4a46d5)
            (*Engine).handleHTTPRequest: context.Next()
    D:/code/src/github.com/gin-gonic/gin/gin.go:273 (0x4a4459)
            (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
    c:/go/src/net/http/server.go:1703 (0x468415)
    c:/go/src/net/http/server.go:1204 (0x466408)
    c:/go/src/runtime/asm_386.s:2287 (0x438ea1)
  • 写回答

2条回答 默认 最新

  • dongwuwan5646 2015-08-05 03:22
    关注

    I understand that I have made mistake with the panic handling. The validation is working if I add the return statement after the c.HTML. This will stop the function from executing the rest of the code. Thanks to @AlexAtNet for his advice and I will keep in mind in future.

    if file == nil {
        log.Println("File is nil.")
        log.Println(err)
        log.Println("*****")
        c.HTML(http.StatusInternalServerError, "index.tmpl", gin.H{
            "title": "Select the input file","error" : "Please select the input file.",
        })   
        return 
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题