Skip to content

Instantly share code, notes, and snippets.

@novalagung
Last active April 8, 2018 02:51
Show Gist options
  • Save novalagung/2ef2ab96d0214bc24ee81e22d6e070fb to your computer and use it in GitHub Desktop.
Save novalagung/2ef2ab96d0214bc24ee81e22d6e070fb to your computer and use it in GitHub Desktop.
if r.Method != "POST" {
http.Error(w, "Only accept POST request", http.StatusBadRequest)
return
}
basePath, _ := os.Getwd()
reader, err := r.MultipartReader()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
fileLocation := filepath.Join(basePath, "files", part.FileName()) // target folder
dst, err := os.Create(fileLocation)
if dst != nil {
defer dst.Close()
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err := io.Copy(dst, part); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
w.Write([]byte(`all files uploaded`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment