GO—-处理文件上传

GO 代码:

package main

import (
	"fmt"
	"html/template"
	"io"
	"log"
	"net/http"
	"os"
)

func saygo(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		t, _ := template.ParseFiles("form.html")
		t.Execute(w, nil)

	} else {
		w.Header().Set("Content-Type", "text/html;charset=utf-8")
		r.ParseMultipartForm(1024 * 1024)
		file, handler, err := r.FormFile("uploadfile")
		if err != nil {
			fmt.Println(err)
			return
		}
		defer file.Close()
		fmt.Fprintf(w, "%v", handler.Header)
		f, err := os.OpenFile("./"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
		if err != nil {
			fmt.Println(err)
			return
		}
		defer f.Close()
		io.Copy(f, file)

		fmt.Fprintf(w, "<br/>")
		t, _ := template.ParseFiles("form.html")
		t.Execute(w, nil)
	}
}

func main() {
	http.HandleFunc("/", saygo)
	err := http.ListenAndServe(":9090", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

模板文件:

<html>
<head>
<title>上传文件</title>
</head>
<body>
<form enctype="multipart/form-data" action="" method="post">
    文件:<input type="file" name="uploadfile"><br/>
    <input type="submit" value="提交">
</form>
</body>
</html>

说明:仅是功能说明,不做于生产使用!

now in order to make your own individual
gay porn Allen Walker Cosplay Costume Makes Your Indicate Far more Interesting

Andriod phone or tablet
miranda lambert weight lossSuit by Suzanne Williams caters to the fashion conscience romantic
youjizz
此条目发表在 网站开发 分类目录,贴了 标签。将固定链接加入收藏夹。

评论功能已关闭。