2

My goal is to try and upload files to a google cloud bucket. However, I am trying to just take it step by step because I struggle with handling files. Right now I'm just trying to send files through formdata and then parse that formdata with busboy.

I know how to send the file once I have it I'm just not sure how to do it with busboy so any help would be great. I'm currently getting the error node:events:498 throw er and Unexpected end of form at Multipart._final. Below is my code and just a side note I am getting my acceptedFiles array from just a react-dropzone component.

I'm more than happy to provide any clarification.

Client-side

const sendFile = async() =>{
  if(acceptedFiles.length > 0){
    for(let filePos = 0;filePos < acceptedFiles.length;filePos++){
      await sendToFireBase(acceptedFiles[filePos])
    }
  }else{
    window.alert("No files have been selected to be uploaded")
  }
}

Endpoint

export default async function UploadToFireStorage(file){
  
    try{

        const formData = new FormData();
        formData.append(file.name,file);

        const response = await fetch(`http://localhost:5000/uploadNewFiles`,{
            method:"POST",
            body: formData
        })
    
        const jsonData = await response.json()
    
        console.log(jsonData)
  
    }catch(error){
  
        console.log(error)
  
    }
  
  }

Server-Side

app.post('/uploadNewFiles',async(req,res)=>{

    console.log('made it to upload')

    try{
        
        if (req.method === 'POST') {
            const busboy = Busboy({ headers: req.headers });
        
            // This callback will be invoked for each file uploaded.
            busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
                try{
                    console.log('got the file');
                    res.json('File made it');
                }catch(error){
                    res.json('F')
                }
            });
        
            busboy.end(req.rawBody);
        } else {
            res.json('Not Post');
        }

    }catch(error){
        res.json("error: " + error)
        console.log(error)
    }

})

0 Answers0