Kaynağa Gözat

tested handler and it can save uploaded file to disk. yes!

master
Patrick Peng Sun 8 yıl önce
ebeveyn
işleme
565f0c971c
2 değiştirilmiş dosya ile 26 ekleme ve 5 silme
  1. +5
    -4
      main.go
  2. +21
    -1
      server.go

+ 5
- 4
main.go Dosyayı Görüntüle

@@ -22,11 +22,12 @@ func main() {
//fs := http.FileServer(http.Dir("/mnt/data/workspace/angular.ts/ng2-admin/dist"))
fs := http.FileServer(http.Dir("/mnt/data/workspace/angular.ts/wechat/ng2-admin/dist"))

http.Handle("/test/", http.StripPrefix("/test", fs))
//http.Handle("/test", http.StripPrefix("/test", fs))
http.Handle("/", fs)
//setup handler
http.HandleFunc("/", webrootHandler)
http.HandleFunc("/api", apiV1Main)
//http.HandleFunc("/", webrootHandler)
//http.HandleFunc("/api", apiV1Main)
http.HandleFunc("/api", uploadHandler)
//http.ListenAndServe("127.0.0.1:65500", nil)
//CreateDefaultMenu()
http.ListenAndServe(":65500", nil)

+ 21
- 1
server.go Dosyayı Görüntüle

@@ -3,11 +3,13 @@ package main
import (
"crypto/sha1"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"sort"
"strings"
)
@@ -161,8 +163,26 @@ func webrootHandler(w http.ResponseWriter, r *http.Request) {

func logRequestDebug(data []byte, err error) {
if err == nil {
fmt.Printf("%s\n\n", data)
fmt.Printf("%s\n\n", string(data))
} else {
log.Fatalf("%s\n\n", err)
}
}

func uploadHandler(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(32 << 20)
file, handler, err := r.FormFile("file")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
fmt.Fprintf(w, "%v", handler.Header)
f, err := os.OpenFile("/tmp/wechat_hitxy_"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
io.Copy(f, file)
}

Yükleniyor…
İptal
Kaydet