Skip to content

Instantly share code, notes, and snippets.

@lepture
Created July 10, 2017 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lepture/c46aaca9f43e20cda9fe7c0701546316 to your computer and use it in GitHub Desktop.
Save lepture/c46aaca9f43e20cda9fe7c0701546316 to your computer and use it in GitHub Desktop.
test-golang-fetch
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test Upload</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file">
<button>Submit</button>
</form>
<script>
var form = document.querySelector('form');
var input = document.querySelector('input');
form.addEventListener('submit', function(e) {
e.preventDefault();
var body = new FormData();
body.append('file', input.files[0]);
fetch('/upload', {method: 'POST', body: body}).then(function(resp) {
console.log(resp.status);
});
});
</script>
</body>
</html>
package main
import (
"net/http"
"log"
)
func main() {
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir(".")))
log.Fatal(http.ListenAndServe(":8010", mux))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment