From 0214e466f3adb6813f799b55567835fa37d917ab Mon Sep 17 00:00:00 2001 From: Andy Date: Wed, 22 Nov 2017 11:11:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BB=A5=E4=B8=8B=E5=8A=9F?= =?UTF-8?q?=E8=83=BD:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.上传文件时,用原来的文件名; 2.增加下载文件到本地功能; --- staticserver.go | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/staticserver.go b/staticserver.go index 1cd7d00..397adb4 100644 --- a/staticserver.go +++ b/staticserver.go @@ -5,10 +5,12 @@ import ( "html/template" "io" "net/http" + "net/url" "os" + "path" "path/filepath" "regexp" - "strconv" + //"strconv" "time" ) @@ -45,11 +47,33 @@ func (*Myhandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if ok, _ := regexp.MatchString("/css/", r.URL.String()); ok { http.StripPrefix("/css/", http.FileServer(http.Dir("./css/"))).ServeHTTP(w, r) } else { - http.StripPrefix("/", http.FileServer(http.Dir("./upload/"))).ServeHTTP(w, r) + filePath := "./upload" + r.URL.String() + filedir, _ := filepath.Abs(filePath) + // 下载文件到本地 + //http.StripPrefix("/", http.FileServer(http.Dir("./upload/"))).ServeHTTP(w, r) + downloadFile(filedir, w) } } +func downloadFile(fileFullPath string, w http.ResponseWriter) { + file, err := os.Open(fileFullPath) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer file.Close() + + fileName := path.Base(fileFullPath) + fileName = url.QueryEscape(fileName) // 防止中文乱码 + w.Header().Set("Content-Disposition", "attachment; filename="+fileName) + _, error := io.Copy(w, file) + if error != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + func upload(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { @@ -67,7 +91,9 @@ func upload(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%v", "不允许的上传类型") return } - filename := strconv.FormatInt(time.Now().Unix(), 10) + fileext + //filename := strconv.FormatInt(time.Now().Unix(), 10) + fileext + // 用原来的文件名 + filename := handler.Filename f, _ := os.OpenFile(Upload_Dir+filename, os.O_CREATE|os.O_WRONLY, 0660) _, err = io.Copy(f, file) if err != nil {