Upload files in batches using Node.js

/ * * *@package The package to introduce is glob axios form-data */
const fs = require('fs');
const glob = require('glob');
const axios = require('axios');
const path = require('path');
const FormData = require('form-data');
const config = require('./utils/config');

// File location
const entry = '.. /build/static/js';

const pattern = path.resolve(__dirname, entry + '/*.js');// Distinguish the environment
const NODE_ENV = process.argv[process.argv.length - 1] | |'production'

const env = config[NODE_ENV]

const files = glob.GlobSync(pattern).found

const formData = new FormData()

formData.append('appid', env.APPID);

files.forEach(element= > formData.append('files', fs.createReadStream(element)) );

const url = `${env.AXIOS_BASE_URL}/api/upload`;

const reqHeaders = {
    headers: formData.getHeaders(),
    onUploadProgress: progressEvent= > {
        let complete = (progressEvent.loaded / progressEvent.total * 100 | 0) + The '%'
        console.log(`\x1B[32m%s\x1B[0m`.Upload Progress:)
        console.log('\x1B[34m%s\x1B[0m', complete)
    }
}
axios.post(url, formData, reqHeaders).then(res= > {
    console.log(res.data)
	// Delete the uploaded file
    files.forEach(element= > fs.unlinkSync(element));
}).catch((res) = > {
    console.log(res)
})
Copy the code