question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamically changing destination folder name

See original GitHub issue

Is it possible to dynamically change the destination folder name depending on the field name? I made a quick test which failed

app.use(multer({
  dest: './uploads/',
  rename: function (fieldname, filename) {
      if(fieldname == 'avatar_file'){
          return filename.replace(/\W+/g, '-').toLowerCase() + Date.now()
      } else {
          return 'avatars/' + filename.replace(/\W+/g, '-').toLowerCase() + Date.now()
      }
  }
}))

Thanks

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:21 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
AndrewKraloveccommented, May 20, 2016

Why not just create a storage script that changes the directory dynamically ? Example of code i use to achieve this .

var multer = require('multer'); //  middleware for handling multipart/form-data,
// Constructor 
module.exports = function (name) {
    try {
        // Configuring appropriate storage 
        var storage = multer.diskStorage({
            // Absolute path
            destination: function (req, file, callback) {
                callback(null, './uploads/'+name);
            },
            // Match the field name in the request body
            filename: function (req, file, callback) {
                callback(null, file.fieldname + '-' + Date.now());
            }
        });
        return storage;
    } catch (ex) {
        console.log("Error :\n"+ex);
    }
}
2reactions
prabhud9468commented, Nov 22, 2019

Dynamically changing destination folder name is done by using formidable and fs

var formidable = require(‘formidable’); var fs = require(‘fs’);

var initial_path = "D:/Files/";
var path=""; //dynamically created path will be stored at runtime
new formidable.IncomingForm().parse(req)
.on('field', (name, field) => {
  //field is an value of dynamic creation folder name
    path = init_path+field+"/";
    console.log(path); //Dynamic path is going to create
    fs.mkdir(path, { recursive: true }, (err) => {
        if (err) throw err;
    });
})
.on('fileBegin',(name, file) => {
    console.log(file.name);
    var a = file.name;
    var b = a.split('.');
    var fname = b[0].replace(/\s/g, '');
    file.path = path + fname+"."+b[1];
    file_path = file.path;
    savedImagePath = file_path.substring(1);
})
.on('file', (name, file) => {
    console.log('Uploaded ' + file.name);
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic - Destination Folder - Power Platform Community
So, when my destination folder is dynamic, i can move all the folders to the correct library. Solved! Go to Solution.
Read more >
How to adjust Dynamic file name save location (Novice)
So in this case, you have to use the "change entire file path" option and you'll need to supply it with a complete...
Read more >
Nprinting Dynamic Naming of folders - Qlik Community - 134604
To create different destination folders you can set the user name, folder or subfolder in the folder destination configuration. Best Regards,.
Read more >
Pass dynamically generated directory name to Destination ...
Pass dynamically generated directory name to Destination attribute of cffile tag ... The upload works fine if I replace the #session.details.
Read more >
2. Get File Names from Source Folder Dynamically in Azure ...
In this video, I discussed about Getting File Names Dynamically from Source folder in Azure Data FactoryLink for Azure Functions Play ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found