Heim  >  Fragen und Antworten  >  Hauptteil

In nodeJS möchte ich einen Standardordner für meine Box-API-Downloadfunktion festlegen

Ich verwende das Box SDK für NodeJS und habe eine Funktion zum Herunterladen von Dateien. Ich muss nur den Download so einstellen, dass er in einem Projektunterordner abgelegt wird Ich habe die Dokumentation gelesen, kann aber keine relevanten Parameter finden

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粉189606269369 Tage vor683

Antworte allen(1)Ich werde antworten

  • P粉668804228

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

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

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

    Antwort
    0
  • StornierenAntwort