在nodeJS中,我想為我的Box API下載功能設定一個預設資料夾
P粉189606269
P粉189606269 2023-09-16 16:35:25
0
1
758

我正在使用 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;
  }
}

P粉189606269
P粉189606269

全部回覆(1)
P粉668804228

我設法解決這個問題,只需將路徑參數傳遞給函數並附加到檔案名稱

const fullPath = path.join(folderPath, fileName);
const writeStream = fs.createWriteStream(fullPath);
fileReadStream.pipe(writeStream);
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板