I'm chunking large uploads in Javascript and trying to parse them to continuously append to disk on Golang.
function fileChunker(file, startChunk) {
const chunkMax = Math.min(startChunk + CHUNK_SIZE, file.size);
const fileChunk = file.slice(startChunk, chunkMax);
const chunkForm = new FormData();
chunkForm.append("file", fileChunk);
chunkUploader(chunkForm, startChunk, file)
}
function chunkUploader(chunkForm, startChunk, file) {
var oReq = new XMLHttpRequest();
oReq.timeout = 2000
oReq.open("POST", "http://localhost:8300/upload", true);
oReq.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
oReq.onload = function (oEvent) {
const updatedStartChunk = startChunk + CHUNK_SIZE;
if (updatedStartChunk < file.size) {
fileChunker(file, updatedStartChunk);
return
}
};
console.log(chunkForm)
oReq.send(chunkForm);
}
package main
import (
"fmt"
"net/http"
)
func uploadHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
if err := r.ParseMultipartForm(0); err != nil {
fmt.Fprintf(w, "Err parsing form.")
}
fmt.Println(r.FormValue("file"))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/upload", uploadHandler)
http.ListenAndServe(":8300", mux)
}
POST iteration in the browser:Prior to sending the request, the form data looks as expected (chunked sizes of 10kb). Trying to print this data is just empty on the server. Is there any way to validate the data I'm sending on the serverside? I feel like this isn't being parsed correctly and I'm not sure why.