Use the http.ServeFile function to send the specified file to the client as the body of the HTTP response.

WBOY
Release: 2023-07-24 23:53:08
Original
1647 people have browsed it

Use the http.ServeFile function to send the specified file to the client as the body of the HTTP response

In the Go language, we can use the http package to quickly build an HTTP server. However, sometimes we need to send a local file to the client as the body of the HTTP response. In order to achieve this function, we can use the http.ServeFile function.

The http.ServeFile function accepts a ResponseWriter and a Request as parameters, and sends the specified file to the client as the body of the HTTP response. It automatically handles the Range header in HTTP requests to support resumed downloads.

The following is an example that shows how to use the http.ServeFile function to send a local file to the client:

package main

import (
    "log"
    "net/http"
)

func serveFile(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "./example.txt")
}

func main() {
    http.HandleFunc("/", serveFile)
    log.Fatal(http.ListenAndServe(":8080", nil))
}
Copy after login

In this example, we create an HTTP server that listens Local port 8080. When an HTTP request is received, it calls the serveFile function to handle the request. The http.ServeFile function is used in the serveFile function to send the "./example.txt" file to the client. Please make sure to save the above example code as a file and place the local "example.txt" file in the same directory.

After running this program, visit http://localhost:8080 in the browser, and you will see that the browser downloaded the "example.txt" file. The Content-Type header of the HTTP response is automatically set based on the file extension, so the file type will be correctly identified.

It should be noted that the http.ServeFile function will automatically process the Range header in the HTTP request when sending the file to the client. If the client requests a part of the file (for example, when resuming a download), the http.ServeFile function will automatically send only the content of that part. This allows us to download large files in multiple batches without having to load the entire file.

In addition to sending the file to the client as the body of the HTTP response, the http.ServeFile function will also automatically handle abnormal situations such as file non-existence and file reading failure, and return the corresponding HTTP error code. Therefore, we don't need to handle these situations manually, which is very convenient.

Summary:
In the Go language, by using the http.ServeFile function, we can easily send local files to the client as the body of the HTTP response. It can not only automatically handle the Range header in HTTP requests, but also handle exceptions such as file non-existence and file read failure. This allows us to quickly build a fully functional static file server.

The above is the detailed content of Use the http.ServeFile function to send the specified file to the client as the body of the HTTP response.. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template