Created
January 28, 2021 16:45
-
-
Save binwiederhier/627f146d1959799be207ad8c17a8f345 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" | |
"log" | |
"net/http" | |
) | |
func main() { | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
log.Printf("GET /") | |
w.Write([]byte(` | |
<html> | |
<input type="file" onchange="upload(this.files[0])"> | |
<script> | |
function upload(file) { | |
console.log('file', file, '--') | |
let xhr = new XMLHttpRequest() | |
xhr.addEventListener('loadstart', function (e) { console.log('loadstart', e, xhr, '--') }) | |
xhr.addEventListener('loadend', function (e) { console.log('loadend', e, xhr, '--') }) | |
xhr.addEventListener('load', function (e) { console.log('load', e, xhr, '--') }) | |
xhr.addEventListener('error', function (e) { console.log('error', e, xhr, '--') }) | |
xhr.addEventListener('abort', function (e) { console.log('abort', e, xhr, '--') }) | |
xhr.addEventListener('readystatechange', function (e) { console.log('readystatechange', e, xhr, '--') }) | |
xhr.upload.addEventListener("progress", function (e) { | |
console.log('progress ' + Math.round((e.loaded * 100.0 / e.total) || 100), e, xhr, '--') | |
}) | |
xhr.open('PUT', 'http://localhost:9999/upload') | |
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest') | |
xhr.overrideMimeType(file.type); | |
xhr.send(file) | |
} | |
</script> | |
</html> | |
`)) | |
}) | |
http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) { | |
log.Printf("PUT /upload") | |
b := make([]byte, 10*1024*1024) | |
n, err := r.Body.Read(b) | |
r.Body.Close() | |
fmt.Printf("read=%d, err=%#v\n", n, err) | |
w.WriteHeader(http.StatusRequestEntityTooLarge) | |
}) | |
http.ListenAndServe(":9999", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment