<< GoLang]的新手,不到10天。我有一个http服务器,我需要http服务磁盘中的文件。默认情况下,这里使用的是“ net / http” http.ServeFile(w,r,file)。 我的问题是当我下载这些文件时,它们没有文件暂停/恢复支持,只是下载而没有显示总大小。我尝试添加“ Content-Length”标头和“ Accept-Ranges”标头] >。但似乎不起作用。 我担心的是HTTP标头,

    内容长度
  • 内容类型
  • 接受范围
  • 内容处置(附件)
  • 我有

    文件路径

  • 信息
FileInfow http.ResponseWriterr http.Request函数。首先我尝试添加
w.Header().Set("Accept-Ranges", "bytes")
if w.Header().Get("Content-Encoding") == "" {
     w.Header().Set("Content-Length", strconv.FormatInt(info.Size(), 10))
}
to

func (s *Server) serveFiles(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(r.URL.Path, "/download/") { url := strings.TrimPrefix(r.URL.Path, "/download/") //dldir is absolute dldir := s.state.Config.DownloadDirectory file := filepath.Join(dldir, url) //only allow fetches/deletes inside the dl dir if !strings.HasPrefix(file, dldir) || dldir == file { http.Error(w, "Nice try\n"+dldir+"\n"+file, http.StatusBadRequest) return } info, err := os.Stat(file) if err != nil { http.Error(w, "File stat error: "+err.Error(), http.StatusBadRequest) return } switch r.Method { case "GET": if info.IsDir() { w.Header().Set("Content-Type", "application/zip") w.WriteHeader(200) //write .zip archive directly into response a := archive.NewZipWriter(w) a.AddDir(file) a.Close() } else { w.Header().Set("Accept-Ranges", "bytes") if w.Header().Get("Content-Encoding") == "" { w.Header().Set("Content-Length", strconv.FormatInt(info.Size(), 10)) } http.ServeFile(w, r, file) }

然后我仍然可以看到它正在下载而不显示总大小,没有暂停/继续支持。

我试图从
下载文件样本小文件:示例大无花果:

可以帮忙吗?

是GoLang的新手,不到10天。我有一个http服务器,我需要http服务磁盘中的文件。默认情况下,这里使用的是“ net / http” http.ServeFile(w,r,file)。我的问题是...