search

Home  >  Q&A  >  body text

In nodeJS I want to set a default folder for my Box API download function

I am using the Box SDK for NodeJS and have a function to download files. I just need to set the download to be placed in a project subfolder I've read the documentation but can't find any relevant parameters

async function downloadBoxFile(fileID, fileName) {
  try {
    // Load configuration
    const config = loadConfiguration();
    const { clientID, clientSecret, enterpriseID } = config.boxConfiguration;

    // Authenticate Box client
    const boxClient = boxAuthentication(clientID, clientSecret, enterpriseID);        

    const fileReadStream = await boxClient.files.getReadStream(fileID, null, {
      fields: 'modified_at, size, sha1, owned_by'
    });

    const writeStream = fs.createWriteStream(fileName);
    fileReadStream.pipe(writeStream);

    return new Promise((resolve, reject) => {
      fileReadStream.on('end', () => {        
        writeLog('> File downloaded successfully:' + fileName);
        resolve();
      });
      fileReadStream.on('error', (error) => {
        console.log('Error downloading file:', error);
        writeLog('Error downloading file:', error);
        reject(error);
      });
    });
  } catch (error) {
    console.log('Error downloading file:', error);
    writeLog('Error downloading file:', error);
    throw error;
  }
}

P粉189606269P粉189606269440 days ago746

reply all(1)I'll reply

  • P粉668804228

    P粉6688042282023-09-17 15:57:39

    I managed to solve this problem by simply passing the path parameter to the function and appending it to the filename

    const fullPath = path.join(folderPath, fileName);
    const writeStream = fs.createWriteStream(fullPath);
    fileReadStream.pipe(writeStream);

    reply
    0
  • Cancelreply