current position:Home>How to upload and download files using Axios in nodejs

How to upload and download files using Axios in nodejs

2022-04-29 08:28:37omage

Download the file

 var res = await axios.get(url,{responseType : "stream"});

Example :

stay koa2 Of get After downloading the file from elsewhere in the request, it is directly transferred back to the front end

const Koa = require('koa');
const Router = require('koa2-router')
const axios = require('axios')

const app = new Koa();
const router = new Router();
const port = 3000;

router.get('/download',async (ctx, next) => {
  const url = <url from other website>
  var res = await axios.get(url,{responseType : "stream"});
  ctx.set("Content-Type", res.headers['content-type']);
  ctx.response.body =;


app.listen(port,() => {
    console.log(`web app started, listening at ${port}`);

Upload files

  Example : After downloading files from another network , Then upload it to another website

const axios = require('axios');
const FormData = require('form-data')

const upload = async () => {
    var url = <the url from other website>;
    var res = await axios.get(url,{responseType : "stream"});
    const formData = new FormData();
    await<uploadUrl>, formData, {
            headers: formData.getHeaders(),
            maxBodyLength: Infinity 

Reference resources :    Send a File With Axios in Node.js - Maxim OrlovEverything you need to know about sending files with axios in Node.js — from creating a form to setting the right headers.

[Solved] Axios Error: Request body larger than maxBodyLength limit | ProgrammerAHicon-default.png?t=M3K6

copyright notice
author[omage],Please bring the original link to reprint, thank you.