A guide on utilizing multer-sftp for downloading files

I've been working on this code, but after searching online I still haven't found a way to download a file from the remote server. I can successfully upload files to the server, but downloading them is posing a challenge.

var storage = sftpStorage({
  sftp: {
    host: '171.16.....',
    port: xxxx,
    username: 'username',
    password: 'xxxxxxxxxxxxxxxx'
  },
    destination: function(req, file, cb) {
       cb(null, 'uploads')
   },
    filename: function(req, file, cb) {
       cb(null, Date.now() + file.originalname)
    }
});
var upload = multer({ storage: storage })

This is the route for uploading files: (Works perfectly!)

router.post('/upload-image', upload.single('file'), listarController.uploadImage);

Now, here's where I'm stuck - the route for downloading files locally: (Still looking for a solution...)

router.get('/get-file/:file', listarController.getFile);

The method used to download files locally:

controller.getFile = (req, res) => {
    var file = req.params.file;
    var path_file = './uploads/' + file;
    fs.exists(path_file, (exists) => {
        if (exists) {
            return res.sendFile(path.resolve(path_file))
        } else {
            return res.status(200).send({
                message: "The image doesn't exist."
            })
        }
    })
}

Would appreciate any suggestions or tips. Thanks in advance!

Answer №1

My solution involved utilizing two key libraries: 1. multer-sftp: Used for uploading the files securely. 2. ftp: Employed for downloading and renaming the necessary files.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The ongoing battle for routing supremacy between Express and Nginx

My current setup involves a unique configuration that I'll need to maintain until I have the opportunity to refactor the project later this year. Right now, it's a static index.html landing page being served with a root location "/" by NGINX. I& ...

sorting in React table not functioning properly for computed column

I have a column titled 'EV/Resource ($ per oz)' that appears to not sort correctly in my react table. Instead of sorting in ascending or descending order, it randomizes the table sort. I'm unsure if I am handling this as a "calculated column ...

Instead of displaying a regular text box, the output unexpectedly shows "Printing [object HTML

There are some HTML and JavaScript files that I have. I've written some functions in my JavaScript file to save the values of different input fields. However, when I try to print the description, it just displays [object HTMLInputElement]. This mak ...

When breaking down code in models, Node.js may display a `var undefined`

As I embark on my journey of creating my first nodejs app, I encountered an error when attempting to transfer a portion of my code to an external JavaScript file. The specific piece of code I am trying to move is the mongodb schema declaration: var mongoo ...

Rendering Information in Angular 4 Through Rest API

Encountering issues displaying data from my local express.js REST API, organized as follows: people: [{ surname: 'testsurname', name: 'testname', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemai ...

Firebase allows for the updating of an object within a nested array

Within Firestore, I have a Document that includes a property named "items" which is of type array. This array consists of ShoppingItem objects with the specified structure: export class ShoppingItem { id?: string; name: string; checked = false; } To ...

JavaScript Function Not Executed in Bottom Section

I've implemented AngularJS includes in my project. Below is the code snippet from my index.html: <!DOCTYPE html> <html ng-app=""> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> ...

What are the best methods for cropping SVG images effectively?

Is it possible to crop a large SVG background that has elements rendered on top of it so that it fits the foreground elements? I am using svg.js but have not been able to find a built-in function for this. Can an SVG be cropped in this way? ...

Issue with updating required Node.js/Express modules using Chokidar?

After browsing through numerous questions and answers regarding chokidar, I am still struggling with an issue. It would be greatly appreciated if someone could help debug my particular code snippet. The Express node app I am working on is running at local ...

Retrieve a div element using two specific data attributes, while excluding certain other data attributes

Here are some examples of divs: <div id="1" data-effect-in="swing" data-effect-out="bounce"></div> <div id="2" data-effect-in="swing"></div> <div id="3" data-effect-out="swing"></div> <div id="4" data-effect-out data ...

Is there a way to switch up the dropdown chevron using only CSS after a click?

Is there a way to change the appearance of dropdown arrows using only CSS and animations when clicked on? I attempted the following method: body { background: #000; } #sec_opt select { /* Reset */ -webkit-appearance: none; -moz-appearance: n ...

Update the network name in the Axios request

Currently, I am utilizing Axios for handling both GET and POST requests. One question that has been on my mind is how to modify the network name: At present, the name serves as the parameter accompanying each request. However, I ponder whether it's f ...

What is the significance of $($(this)) in coding jargon

While browsing the internet, I came across a code snippet that includes the following line: if ($($(this)).hasClass("footer_default")) { $('#abc') .appendTo($(this)) .toolbar({position: "fixed"}); } I'm curious ab ...

Operating a Redis queue within a Node.js cluster setting

In my Node.js cluster setup, I want the master process to generate data items and add them to a Redis queue. The goal is to have multiple worker processes reading from this queue but only one worker should consume each item. I am looking for guidance on w ...

Guide on adding information to an array field in MongoDB with Node.js

I want to update the articles attribute by adding the titles of different articles. The function in my user.controller.js file looks like this: module.exports.savearticle = (req, res, next) => { User.findOneAndUpdate( req._id, { $addToS ...

Learn how to effectively display checkboxes in an HTML form with Vue configuration

new Vue({ el: '...', data: { checkeditems: [] } }) <div v-for="item in instituteModel" v-if="instituteModel.length > 0"> <input type="checkbox" id="item.id" value="item.inst_name" v-model="checkeditems"/> </div&g ...

What is the best way to use multiple encoding types when sending data via a POST method in Node.js?

My current challenge involves sending a combination of name and description text data alongside a video file. Unfortunately, I've encountered an issue where I can only send either the video or the text, but not both simultaneously. Below is the code ...

I pressed the button but the globalCompositeOperation isn't working as expected. How can I make it function correctly to achieve the desired output like the second canvas?

<!DOCTYPE html> <html> <head> <title>Canvas Compositing Tutorial</title> </head> <body> <canvas id="myCanvas" width="200" height="200" style="border: 1px solid"></canvas> <br> <butt ...

Identify when the Vue page changes and execute the appropriate function

Issue with Map Focus Within my application, there are two main tabs - Home and Map. The map functionality is implemented using OpenLayers. When navigating from the Home tab to the Map tab, a specific feature on the map should be focused on. However, if th ...

Tips on navigating an array to conceal specific items

In my HTML form, there is a functionality where users can click on a plus sign to reveal a list of items, and clicking on a minus sign will hide those items. The code structure is as follows: <div repeat.for="categoryGrouping of categoryDepartm ...