0

I'm uploading files through google drive API resumable upload as multiple chunks to monitor the upload state. So I used for loop to request for each chunk. The full code is as below :

xhr.onreadystatechange = async function () {

    if (this.readyState == this.HEADERS_RECEIVED) {
      // Get the raw header string
      var headers = xhr.getAllResponseHeaders();
      // Convert the header string into an array
      // of individual headers
      var arr = headers.trim().split(/[\r\n]+/);
      // Create a map of header names to values
      var headerMap = {};
      arr.forEach(function (line) {
        var parts = line.split(": ");
        var header = parts.shift();
        var value = parts.join(": ")
        headerMap[header] = value;
      });
      const resumableURI = headerMap.location
      console.log('resumable uri : ' + resumableURI)
      const fileChunks = divideFileInChunks(req.files.muploadedFile.data)
      
      try {
        for( let i =0; i<fileChunks.length; i+=1) {
           console.log('inside for loop');
             axios.put(`${resumableURI}`,  bufferToStream(buffer),
              {
               headers: {
                 Authorization: `Bearer ${accessToken }`,
               "Content-Length": req.files.muploadedFile.size,
               "Content-Range": `bytes ${fileChunks.start}-${fileChunks.end }/${req.files.muploadedFile.size}`
                 } 
             }).then( resp => {
                    console.log(resp.data);
                  }).catch( e => console.log(e))
            return res.status(200).json({'message' : `${i+1} Chunk Uploaded of ${fileChunks.length}`})             
            }
        } catch (f) {
        res.send(f.message);
      }

             
    } 
  };
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.setRequestHeader("Authorization", `Bearer ${accessToken }`);
xhr.send( //3
    JSON.stringify({
      name: req.files.muploadedFile.name,
      mimeType: req.files.muploadedFile.mimetype, 
    })
  );

});

The problem here is the code inside for loop is not executing.

Masoud
  • 375
  • 1
  • 5
  • 16
karthi keyan
  • 1
  • 1
  • 4
  • 1
    Well, you have a `return` inside the loop's body. So it will exit after the first iteration. – derpirscher Jul 17 '22 at 17:14
  • Thanks for response. I checked for loop execution by logging message from inside the loop, but message is not logging to the console. Thus first iteration itself is not executing. What you told will also be problem. Please suggest me solution for them both. Or a alternate code. Once again thanks for response. – karthi keyan Jul 17 '22 at 17:48
  • If the response is returned outside the loop's body i is undefined. How could i send message to client about upload status? Any suggestions? – karthi keyan Jul 17 '22 at 17:57
  • The only possibility that the for loop doesn't execute even a single time is, that you don't reach that part of the code at all because `divideFileInChunks` throws an error or returns an empty array/undefined/null. – derpirscher Jul 17 '22 at 18:17
  • Thank you @derpirscher. divideFileInChunks returns 0 , so code is not executing. – karthi keyan Jul 20 '22 at 04:30

0 Answers0