Browse Source

获取传送文件大小

master
453530270@qq.com 2 years ago
parent
commit
9072959e9f
  1. 35
      xgfs/internal/handler/handler.go
  2. 11
      xgfs/main.go
  3. BIN
      xgfs/xtfs

35
xgfs/internal/handler/handler.go

@ -28,6 +28,41 @@ type Args struct {
Zpfile, Fname string
}
// 获取文件大小的接口
type Size interface {
Size() int64
}
// 获取文件信息的接口
type Stat interface {
Stat() (os.FileInfo, error)
}
// 显示状态
func ReceiveStatus(w http.ResponseWriter, r *http.Request) {
switch r.Method {
// 显示当前传送文件的大小
case http.MethodPost:
file, _, err := r.FormFile("file")
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if statInterface, ok := file.(Stat); ok {
fileInfo, _ := statInterface.Stat()
fmt.Fprintf(w, "上传文件的大小为: %d", fileInfo.Size())
}
if sizeInterface, ok := file.(Size); ok {
fmt.Fprintf(w, "上传文件的大小为: %d", sizeInterface.Size())
}
return
}
// fmt.Fprintf(w, r.Method)
}
// 接收 上传
func ReceiveHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {

11
xgfs/main.go

@ -33,12 +33,13 @@ func receiveClient() error {
// fmt.Printf("Server address: %s\n", address)
// 注释上面的局域网广播
fmt.Println("xtfs run as receive role...")
// 显示状态等
http.HandleFunc("/", handler.ReceiveStatus)
// http.HandleFunc("/upload", handler.ReceiveHandler)
// http.Handle("/static/", http.StripPrefix("/static/",
// http.FileServer(http.FS(web.StaticFs))))
http.HandleFunc("/", handler.ReceiveHandler)
http.Handle("/static/", http.StripPrefix("/static/",
http.FileServer(http.FS(web.StaticFs))))
fmt.Println("Waiting for transfer...")
fmt.Println("Waiting for receive...")
return http.ListenAndServe(config.G.WildcardAddress, nil)
}

BIN
xgfs/xtfs

Binary file not shown.
Loading…
Cancel
Save