Friday 5 April 2024

c.file, r.file

 https://pkg.go.dev/github.com/gin-gonic/gin#Context.File


c.File and r.Static serve similar purposes but are used in different contexts in Gin:


c.File: This method is used within a route handler to send a single file as the HTTP response. It's typically used when you want to serve a specific file in response to a request. You provide the file path as an argument to c.File(filePath).


Example:


go

Copy code

func main() {

    r := gin.Default()


    r.GET("/download", func(c *gin.Context) {

        c.File("./files/example.txt")

    })


    r.Run(":8080")

}

r.Static: This function is used to serve static files from a directory. It's typically used to serve multiple files from a specified directory in response to requests for static content. You provide the URL path prefix and the directory path as arguments to r.Static(urlPathPrefix, directoryPath).



!!!!!! they both do not support resumable download , but HTTP does

Gin does not directly support pausing and resuming file transmission. When you use c.File to serve a file, it streams the file's content to the client as an HTTP response. However, HTTP itself does support range requests, which allow clients to request specific portions of a resource. This feature can be used to implement resumable downloads.


If you need to implement pause and resume functionality for file transmission in Gin, you would need to handle range requests manually. You can parse the Range header from the client's request and serve the appropriate portion of the file based on the requested range. This involves setting appropriate headers in the response to indicate the range being served (Content-Range) and the total size of the file (Content-Length), as well as returning the appropriate status code (206 Partial Content).

------------------


c *gin.Context

func (*Context) File 

func (c *Context) File(filepath string)
c.File
File writes the specified file into the body stream in an efficient way.

https://gin-gonic.com/docs/examples/serving-static-files/

func main() {
	router := gin.Default()
	router.Static("/assets", "./assets")
	router.StaticFS("/more_static", http.Dir("my_file_system"))
	router.StaticFile("/favicon.ico", "./resources/favicon.ico")

	// Listen and serve on 0.0.0.0:8080
	router.Run(":8080")
}

No comments:

Post a Comment