Giter Site home page Giter Site logo

Comments (21)

AndrewKralovec avatar AndrewKralovec commented on April 19, 2024 4

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);
    }
}

from multer.

prabhud9468 avatar prabhud9468 commented on April 19, 2024 2

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);
})

from multer.

hacksparrow avatar hacksparrow commented on April 19, 2024

You can't. However, you can move the file to other locations after the file is uploaded.

from multer.

Mindstormer619 avatar Mindstormer619 commented on April 19, 2024

@kokujin If you are still interested, I managed to do this with this small addition:

"rename" : function (fieldname, filename, req, res) {
    return path.join(fieldname, "i" + req.params.user_id);
}

(Saves it like <defaultFolder>/<fieldName>/i564221sdd456.jpg. I added the "i" because some stuff might complain with filenames starting with numbers)

I got the idea after looking at multer's source code. It seems that it does a path.join() to get the full path using the destination folder and the uploaded filename. Adding this works for me.

Note that this might change in the future, breaking this code? @hacksparrow?

from multer.

Mindstormer619 avatar Mindstormer619 commented on April 19, 2024

Actually is there a specific reason why fieldnames and filenames cannot be passed to changeDest()?

from multer.

LinusU avatar LinusU commented on April 19, 2024

@Mindstormer619 I think that the problem with using fieldnames is that they might not be available when the file comes. Most browsers sends the fields and files in the order they are placed in the DOM. So if the field for the name is after the file input field, multer won't know about it when it's receiving the file.

from multer.

Mindstormer619 avatar Mindstormer619 commented on April 19, 2024

Ohhh, interesting. So I'm taking it that the rename feature works after the file is done uploading?

from multer.

Mindstormer619 avatar Mindstormer619 commented on April 19, 2024

Hmm, looking at the source it doesn't seem to be a problem to be including the field name as a param for the changeDest() function. I could be wrong about this. Can someone confirm?

from multer.

Mindstormer619 avatar Mindstormer619 commented on April 19, 2024

@LinusU I'm fairly sure that multer is well aware of the fieldname before the file is uploaded, as can be referenced by file.fieldname within the onFileUploadStart function

from multer.

LinusU avatar LinusU commented on April 19, 2024

@Mindstormer619 Ahh, I think I misunderstood. I thought the question was about renaming it to the value of another field. The fieldname is known to multer at that point, although not passed to the changeDest function.

Your workaround should work 👍

from multer.

Mindstormer619 avatar Mindstormer619 commented on April 19, 2024

@LinusU My workaround does work 😛 . However I'm asking about it being added directly to the source. Sort of like changing changeDest(dest, req, res) to changeDest(dest, req, res, fieldname).

from multer.

LinusU avatar LinusU commented on April 19, 2024

This will be fixed by #131, specifically by #126 and #123

from multer.

pommyk avatar pommyk commented on April 19, 2024

@Mindstormer619 : This does not seem to work for me, the upload occurs, but only to the default directory. Warning: nodejs noob.

var express = require('express');
var multer  = require('multer');
var fs = require('fs');
//var upload = multer({ dest: 'uploads/' });

var app = express();

app.use(express.static(__dirname + "/public"));

app.post('/profile', multer({
    dest: 'uploads/', rename: function(fieldname, filename, req, res){
        var newDestination = path.join(req.body.hdnUserName);
        return newDestination;
    }}).single('uploadedFile'), function (req, res, next) {
        res.end('over');
    });

app.listen(8080, function(e){console.log('Server is listening on port 8080')});

from multer.

LinusU avatar LinusU commented on April 19, 2024

#188 would probably have helped here. Your problem is that rename should be sent to the storage, not multer.

Try this:

var express = require('express')
var multer  = require('multer')
var fs = require('fs')

var storage = multer.diskStorage({
  destination: 'uploads/',
  filename: function (req, file, cb) {
    cb(null, req.body.hdnUserName)
  }
})
var upload = multer({ storage: storage })
var app = express()

app.use(express.static(__dirname + "/public"))
app.post('/profile', upload.single('uploadedFile'), function (req, res, next) {
  res.end('over')
})

app.listen(8080, function () {
  console.log('Server is listening on port 8080')
})

from multer.

pommyk avatar pommyk commented on April 19, 2024

@LinusU: Thanks for bringing up #188. This helped me get what i wanted thanks!

from multer.

Maltemo avatar Maltemo commented on April 19, 2024

@AndrewKralovec answers should be in the README.md file as an example
I think it would help a lot of people and help them to spend less time searching for this functionnality

from multer.

codertapsu avatar codertapsu commented on April 19, 2024

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);
    }
}

Thx so much!
___Arigato!

from multer.

kancharla-sandeep avatar kancharla-sandeep commented on April 19, 2024

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);

Can you explain me where to call this script

from multer.

AndrewKralovec avatar AndrewKralovec commented on April 19, 2024

@kancharla-sandeep , sure thing. Please excuses the code quality, this project was from college, and it was before i even knew what a promise was.

https://github.com/AndrewKralovec/Alpha-Learning/search?q=storage&unscoped_q=storage

from multer.

Akhilbmsb avatar Akhilbmsb commented on April 19, 2024

@prabhud9468 thanks for the solution, i was exactly looking for the similar kind of solution.

from multer.

Jean-Baptiste-Lasselle avatar Jean-Baptiste-Lasselle commented on April 19, 2024

#188 would probably have helped here. Your problem is that rename should be sent to the storage, not multer.

Try this:

var express = require('express')
var multer  = require('multer')
var fs = require('fs')

var storage = multer.diskStorage({
  destination: 'uploads/',
  filename: function (req, file, cb) {
    cb(null, req.body.hdnUserName)
  }
})
var upload = multer({ storage: storage })
var app = express()

app.use(express.static(__dirname + "/public"))
app.post('/profile', upload.single('uploadedFile'), function (req, res, next) {
  res.end('over')
})

app.listen(8080, function () {
  console.log('Server is listening on port 8080')
})

Hi, I tried your snippet code, and well it does not even compile :

> [email protected] build /home/scrapbook/tutorial/pokus
> tsc -p src

src/controllers/file.controller.ts(37,62): error TS2339: Property 'body' does not exist on type 'Request'.
  • my multer config :
    const pokusStorageOnDisk = multer.diskStorage({
      destination: function(req, file, cb) {
          // le répertoire [workspace/pokus] doit exister
          cb(null, 'workspace/pokus');
      },

      // By default, multer removes file extensions so let's add them back
      filename: function(req, file, cb) {
        console.log(" Valeur chopee : [" + file.fieldname + '-' + Date.now() + path.extname(file.originalname) + "]");
        console.log(" Valeur file.originalname : [" + file.originalname + "]");
        console.log(" Valeur file.path : [" + file.path + "]");
        console.log(" Valeur path.extname(file.originalname) : [" + path.extname(file.originalname) + "]");
        console.log(" Valeur req.body.hdnUserName : [" + req.body.hdnUserName + "]");
        cb(null, file.originalname);
      }
    });

So maybe your code compiled once.

from multer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.