golang 下载文件

//File is found, create and send the correct headers

//Get the Content-Type of the file
//Create a buffer to store the header of the file in
FileHeader := make([]byte, 512)
//Copy the headers into the FileHeader buffer
Openfile.Read(FileHeader)
//Get content type of file
FileContentType := http.DetectContentType(FileHeader)

//Get the file size
FileStat, _ := Openfile.Stat()                     //Get info from file
FileSize := strconv.FormatInt(FileStat.Size(), 10) //Get file size as a string

//Send the headers
writer.Header().Set("Content-Disposition", "attachment; filename="+Filename)
writer.Header().Set("Content-Type", FileContentType)
writer.Header().Set("Content-Length", FileSize)

你可能感兴趣的:(golang 下载文件)