Last active
November 29, 2018 02:21
-
-
Save Jimeux/39acf40402b5156fd97dade66c183b6d 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
package main | |
import ( | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"net/http/httputil" | |
"os" | |
"time" | |
) | |
func main() { | |
http.HandleFunc("/upload", handleUpload) | |
log.Fatal(http.ListenAndServe(":3000", nil)) | |
} | |
func handleUpload(w http.ResponseWriter, r *http.Request) { | |
if r.Method != http.MethodPost { | |
w.WriteHeader(http.StatusNotFound) | |
return | |
} | |
// リクエストの情報を出力する | |
requestDump, err := httputil.DumpRequest(r, true) | |
handleError(err) | |
log.Println(string(requestDump)) | |
// "file"というフィールド名に一致する最初のファイルが返却される | |
// マルチパートフォームのデータはパースされていない場合ここでパースされる | |
formFile, _, err := r.FormFile("file") | |
handleError(err) | |
defer formFile.Close() | |
// データを保存するファイルを開く | |
filename := fmt.Sprintf("uploaded_%d.txt", time.Now().UnixNano()) | |
saveFile, err := os.Create(filename) | |
handleError(err) | |
defer saveFile.Close() | |
// ファイルにデータを書き込む | |
_, err = io.Copy(saveFile, formFile) | |
handleError(err) | |
w.WriteHeader(http.StatusCreated) | |
} | |
func handleError(err error) { | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment