New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamically changing destination folder name #39
Comments
You can't. However, you can move the file to other locations after the file is uploaded. |
@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 I got the idea after looking at multer's source code. It seems that it does a Note that this might change in the future, breaking this code? @hacksparrow? |
Actually is there a specific reason why fieldnames and filenames cannot be passed to |
@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. |
Ohhh, interesting. So I'm taking it that the |
Hmm, looking at the source it doesn't seem to be a problem to be including the field name as a param for the |
@LinusU I'm fairly sure that multer is well aware of the fieldname before the file is uploaded, as can be referenced by |
@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 Your workaround should work 👍 |
@LinusU My workaround does work 😛 . However I'm asking about it being added directly to the source. Sort of like changing |
@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')}); |
#188 would probably have helped here. Your problem is that 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')
}) |
Why not just create a storage script that changes the directory dynamically ?
|
@AndrewKralovec answers should be in the |
Thx so much! |
Can you explain me where to call this script |
@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 |
Dynamically changing destination folder name is done by using formidable and fs var formidable = require('formidable');
|
@prabhud9468 thanks for the solution, i was exactly looking for the similar kind of solution. |
Hi, I tried your snippet code, and well it does not even compile : > myapp@0.0.1 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'.
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. |
Is it possible to dynamically change the destination folder name depending on the field name? I made a quick test which failed
Thanks
The text was updated successfully, but these errors were encountered: