Sails.JS Download files using a skipper, access to the original file name and dynamic download path

I want to upload a simple file using a skipper. Previously, files uploaded through forms could be retrieved using req.files, but they learned that in Sails 0.10.x with the default gateway, req.files is undefined. Instead, req.file ('filename') is used to access the file.

I could do a simple file upload by looking at the documentation. However, I want to have access to the file name before downloading it, as well

  • Upload the file to a dynamically created directory based on the user who uploaded it
  • The name of the newly created file should be oldFileName + _ + timestamp in integer

How can I do this with the skipper module, or rather, the most efficient way to do this?

EDIT
So far, I have been able to do this, but I hope that there should be a better way.

I used this expression to access the file name

var inputFileName = req.file('inputFile')._files[0]["stream"]["filename"]; 

I could see that the skipper automatically creates a directory if it is not based on the path to the file and the name specified in the parameter to load the function

+6
source share
1 answer

At the moment, the problem is in Skipper - there are 2 Pull-Requests for this ( https://github.com/balderdashy/skipper/issues/12 ) - but I do not know when it is combined and available.

To solve your problem:

1. Overrwrite filename

 // save original file name var origifile = req.file('testfile')._files[0].stream.filename; var filename = origifile; // check if file is existing and change filename if necessary while(fs.existsSync(".tmp/uploads/" +filename)){ // Add 4 random chars at he beginning of the filename filename = randString(3)+"_"+origifile; }; req.file('testfile').upload(filename,function (err, files) { 

Note: you need to write the randString () command - Function

2. Move the downloaded file to a folder

In the upload callback:

 fs.rename("/tmp/uploads/"+files[0].filename, "your_folder/" +files[0].filename, function(err){ .... }); 
+9
source

Source: https://habr.com/ru/post/971761/


All Articles