我想在nodeJS中为我的Box API下载功能设置一个默认文件夹

问题描述 投票:0回答:1

我正在使用 NodeJS 的 Box SDK,并且有一个下载文件的功能。 我只是 需要将下载设置放在项目子文件夹中 我已阅读文档,但找不到任何参数

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;
  }
}
javascript node.js box-api boxsdk
1个回答
0
投票

我设法解决它,只需将路径参数传递给函数并附加到文件名

const fullPath = path.join(folderPath, fileName);
const writeStream = fs.createWriteStream(fullPath);
fileReadStream.pipe(writeStream);
© www.soinside.com 2019 - 2024. All rights reserved.