vue代码:
将搜索的nginx日志文件拖到此处,或点击上传
golang代码:
func UploadHandler(w http.ResponseWriter, r *http.Request)
GetFile(w, r, "../test.dat")
}
func GetFile(w http.ResponseWriter, r *http.Request, filepath string) {
fmt.Println("开始接收文件******")
//解决跨域问题
if origin := r.Header.Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Credentials", "true");
}
r.ParseMultipartForm(32 << 20)
//解决上传多个文件问题,每个文件分别保存
file, fileinfo_w, err := r.FormFile("userfile")
if err != nil {
fmt.Println("接收文件异常: ", err)
return
}
if fileinfo_w != nil {
defer file.Close()
os.Remove(filepath)
f, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println("创建文件异常: ", err)
return
}
defer f.Close()
io.Copy(f, file)
ResponseWithOrigin(w, r, http.StatusOK)
fmt.Fprint(w, "上传成功!")
ResponseWithOrigin(w, r, http.StatusOK)
fmt.Println("完成文件接收******")
}
}
func ResponseWithOrigin(w http.ResponseWriter, r *http.Request, code int) {
w.Header().Set("Access-Control-Allow-Origin", "*") //允许访问所有域
w.Header().Add("Access-Control-Allow-Headers", "Content-Type") //header的类型
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Access-Control-Allow-Credentials", "true");
w.WriteHeader(code)
//w.Write(json)
}
作者:vue从入门到放弃