实现一个简单的页面上传文件功能。
/**
* MyFileUpload01
* @Author: Jian Junbo
* @Email: junbojian@qq.com
* @Create: 2017/9/17 15:14
* Copyright (c) 2017 Jian Junbo All rights reserved.
*
* Description: 简单的上传文件
*/
package main
import (
"net/http"
"fmt"
"os"
"io"
"time"
"path"
"strconv"
)
func main() {
http.HandleFunc("/", index)
http.HandleFunc("/upload2", upload)
err := http.ListenAndServe(":7373", nil)
if err != nil{
fmt.Println("服务器启动失败",err.Error())
return
}
}
func upload(writer http.ResponseWriter, request *http.Request) {
request.ParseMultipartForm(32<<20)
//接收客户端传来的文件 uploadfile 与客户端保持一致
file, handler, err := request.FormFile("uploadfile")
if err != nil{
fmt.Println(err)
return
}
defer file.Close()