Last active
April 8, 2018 02:51
-
-
Save novalagung/2ef2ab96d0214bc24ee81e22d6e070fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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