Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/krilie/go-sfile

s-file gin upload file to local path and delete file
https://github.com/krilie/go-sfile

file gin go golang upload-file

Last synced: 4 days ago
JSON representation

s-file gin upload file to local path and delete file

Awesome Lists containing this project

README

        

# s-file
s-file gin upload file to local path and delete file

## use
go get github.com/krilie/s-file

## example
```golang
func main() {
sfile := s_file.NewSFile("./files")
gin.SetMode(gin.DebugMode)
engine := gin.New()
engine.POST("/upload", func(c *gin.Context) {
fileHeader, err := c.FormFile("file")
if err != nil {
c.JSON(400, gin.H{"msg": err.Error()})
return
}
log.Printf("upload file %v", fileHeader.Filename)
file, err := fileHeader.Open()
if err != nil {
c.JSON(400, gin.H{"msg": err.Error()})
return
}
defer file.Close()
content, key, err := sfile.SaveFile(c, fileHeader.Filename, file)
if err != nil {
c.JSON(400, gin.H{"msg": err.Error()})
return
}
c.JSON(200, gin.H{"msg": content, "key": key, "size": fileHeader.Size})
return
})
engine.POST("/delete/:key", func(c *gin.Context) {
err := sfile.DeleteFile(c, c.Param("key"))
if err != nil {
c.JSON(400, gin.H{"msg": err.Error()})
return
}
c.JSON(200, gin.H{"msg": "ok"})
return
})
err := engine.Run(":80")
log.Println(err)
}
```