首先,我们在服务器端设定两个路由,/upload用于文件上传,/files/*用于文件下载。
- const maxUploadSize = 2 * 1024 * 2014 // 2 MB
- const uploadPath = "./tmp"
- funcmain() {
- http.HandleFunc("/upload", uploadFileHandler())
- fs := http.FileServer(http.Dir(uploadPath))
- http.Handle("/files/", http.StripPrefix("/files", fs))
- log.Print("Server started on localhost:8080, use /upload for uploading files and /files/{fileName} for downloading files.")
- log.Fatal(http.ListenAndServe(":8080", nil))
- }
现在我们只需要实现uploadFileHander.这个处理程序将包含以下功能:
·验证文件最大值;
·从请求验证文件和POST参数
·检查所提供的文件类型
·创建一个随机文件名
·将文件写入硬盘
·处理所有错误,如果一切顺利返回成功消息
第一步,我们定义处理程序:
- funcuploadFileHandler()http.HandlerFunc {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize)
- if err := r.ParseMultipartForm(maxUploadSize); err != nil {
- renderError(w, "FILE_TOO_BIG", http.StatusBadRequest)
- return
- }
- fileType := r.PostFormValue("type")
- file, _, err := r.FormFile("uploadFile")
- if err != nil {
- renderError(w, "INVALID_FILE", http.StatusBadRequest)
- return
- }
- defer file.Close()
- fileBytes, err := ioutil.ReadAll(file)
- if err != nil {
- renderError(w, "INVALID_FILE", http.StatusBadRequest)
- return
- }
幸运的是,Go标准库提供给我们一个Http.DetectConntectType函数,这个函数基于mimesniff算法,祝需要读取文件的512个字节就能够判断文件的类型。
- iletype := http.DetectContentType(fileBytes)
- if filetype != "image/jpeg" && filetype != "image/jpg" &&
- filetype != "image/gif" && filetype != "image/png" &&
- filetype != "application/pdf" {
- renderError(w, "INVALID_FILE_TYPE", http.StatusBadRequest)
- return
- }
- fileName := randToken(12)
- fileEndings, err := mime.ExtensionsByType(fileType)
- if err != nil {
- renderError(w, "CANT_READ_FILE_TYPE", http.StatusInternalServerError)
- return
- }
- newPath := filepath.Join(uploadPath, fileName+fileEndings[0])
- fmt.Printf("FileType: %s, File: %s ", fileType, newPath)
如果所有部分都没问题,我们给用户返回一个i额SUCCESS信息。
- newFile, err := os.Create(newPath)
- if err != nil {
- renderError(w, "CANT_WRITE_FILE", http.StatusInternalServerError)
- return
- }
- defer newFile.Close()
- if _, err := newFile.Write(fileBytes); err != nil {
- renderError(w, "CANT_WRITE_FILE", http.StatusInternalServerError)
- return
- }
- w.Write([]byte("SUCCESS"))