I am trying to build a small app that allows you to upload large files in chunks. I will add the ability to pause later as well.
I am stuck on this error inside my loop: Error processing file:multipart: NextPart: EOF
func main() {
    router := gin.Default()
    rg := router.Group("api/v1")
    {
        rg.POST("/photo", uploadFile)
    }
    router.Use(CORSMiddleware())
    router.Run()
}
func uploadFile(c *gin.Context) {
    mr, e := c.Request.MultipartReader()
    if e != nil {
        panic("Error reading request:" + e.Error())
    }
    client, e := storage.NewClient(c, option.WithAPIKey(uploadApiKey))
    bucket := client.Bucket(uploadBucket)
    for {
        p, e := mr.NextPart()
        if e == io.EOF {
            break
        } else if e != nil {
            panic("Error processing file:" + e.Error())
        }
        w := bucket.Object(p.FileName()).NewWriter(c)
        if _, e := io.Copy(w, p); e != nil {
            panic("Error during chunk upload:" + e.Error())
        } else if e := w.Close(); e != nil {
            panic("Could not finalize chunk writing:" + e.Error())
        }
    }
}
The Client-side code looks like this:
class FileToUpload {
    static chunkSize = 512000;
    static uploadUrl = 'http://localhost:8080/api/v1/photo';
    readonly request: XMLHttpRequest;
    readonly file: File;
    readonly name: string;
    currentChunkStartByte: number;
    currentChunkFinalByte: number;
    constructor(file: File, name: string) {
        this.request = new XMLHttpRequest();
        this.file = file;
        this.name = name;
        this.currentChunkStartByte = 0;
        this.currentChunkFinalByte = FileToUpload.chunkSize > this.file.size ? this.file.size : FileToUpload.chunkSize;
    }
    uploadFile() {
        let chunk: Blob = this.file.slice(this.currentChunkStartByte, this.currentChunkFinalByte);
        this.request.overrideMimeType('application/octet-stream');
        this.request.open('POST', FileToUpload.uploadUrl, true);
        const randomNum = Math.random().toString().substr(2);
        this.request.setRequestHeader('Content-Type', 'multipart/form-data; boundary=--'+randomNum);
        this.request.setRequestHeader('Content-Range', `bytes ${this.currentChunkStartByte}-${this.currentChunkFinalByte}/${this.file.size}`);
        this.request.onload = () => {
            if(this.currentChunkFinalByte === this.file.size) {
                // Do something once done with file
                return;
            }
            this.currentChunkStartByte = this.currentChunkFinalByte;
            this.currentChunkFinalByte = this.currentChunkStartByte + FileToUpload.chunkSize;
            this.uploadFile();
        }
        this.request.send(chunk);
    }
}
I already have a check for EOF, I don't understand why I still get this error. Any ideas?
 
    