Comments (6)
The typings are not precise - it just says: Express.Multer.File
but in fact there are two stages with different sets of fields:
- At
multer.diskStorage.filename
stage thefile
contaons only 4 fields:fieldname, originalname, encoding, mimetype
as mentioned here
AtdiskStorage.on finish
when the file finished to be written to the disk, thefile
param is updated with additional fields from here withdestination, filename, path, size
fields here
So, actually we can't rely on typings indiskStorage.filename
hook - For providing a random name you can send an updated filename in
cb(null, ``${ file.originalname }-${Math.random()}``)
filename: function (req, file, cb) {
cb(null, path.join(`${ TMP_DIR }/${ file.originalname }/${hash.digest("hex")}`))
}
So you don't need to stream the file to some additional location - by passing a correct path
argument to the cb
you'll get the file wherever you need
from multer.
Sorry for the late reply:
After the upload, I receive the file buffer
In fact, despite the downsides of using MemoryStorage, it'll work - if the file exists, you just discard the buffer; if it doesn't you can write the buffer to the disk to the relevant directory
upload process will be performed twice if the file does not exist
No, in this case the file will be uploaded and processed once - you upload it to MemoryStorage and then write it to the disk or discard
we should delete it from the temporary file directory
In this case you don't need a temp directory - you can save the buffer directly to the needed directory
You're welcome, dear friend. Thank you for the comprehensive and thorough points. I will definitely apply the changes to my code.
from multer.
The typings are not precise - it just says:
Express.Multer.File
but in fact there are two stages with different sets of fields:
- At
multer.diskStorage.filename
stage thefile
contaons only 4 fields:fieldname, originalname, encoding, mimetype
as mentioned here
AtdiskStorage.on finish
when the file finished to be written to the disk, thefile
param is updated with additional fields from here withdestination, filename, path, size
fields here
So, actually we can't rely on typings indiskStorage.filename
hook- For providing a random name you can send an updated filename in
cb(null,
${ file.originalname }-${Math.random()})
filename: function (req, file, cb) { cb(null, path.join(`${ TMP_DIR }/${ file.originalname }/${hash.digest("hex")}`)) }So you don't need to stream the file to some additional location - by passing a correct
path
argument to thecb
you'll get the file wherever you need
Thank you.
I wanted to retrieve the file buffer before it is uploaded. The approach I took to retrieve the file buffer was actually wrong. To obtain the file buffer before uploading, I needed to set the value of the variable storage
to multer.memoryStorage()
like this:
const storage = multer.memoryStorage();
const upload = multer({
storage,
});
Soon, I will make this part of my project open source on GitHub and share the link below this post.
from multer.
- For my point of view, your previous approach was correct and
multer
does it already: gets the file and stream it to some directory. Unlike some other languages, in Nodejs it's a bit more low-level, and you need to provide the upload destination, to ensure it's available etc.
So you can create some temp dir, stream there the file, adjust the name, perform checks and everything you might need to do for your uploading strategy - MemoryStorage is a bit tricky:
i. You'll need to have enough available memory for file uploading
ii. It's a bit more fragile since if the process crashes the Buffer contains the file will be lost
from multer.
- For my point of view, your previous approach was correct and
multer
does it already: gets the file and stream it to some directory. Unlike some other languages, in Nodejs it's a bit more low-level, and you need to provide the upload destination, to ensure it's available etc.
So you can create some temp dir, stream there the file, adjust the name, perform checks and everything you might need to do for your uploading strategy- MemoryStorage is a bit tricky:
i. You'll need to have enough available memory for file uploading
ii. It's a bit more fragile since if the process crashes the Buffer contains the file will be lost
Thank you for your guidance.
Let me explain a scenario based on your instructions. For example, I send a file to the server and upload it to a temporary directory. After the upload, I receive the file buffer and calculate its hash, then search for the hash among the files in the directories.
If the hash exists, I delete the temporary file; otherwise, if the hash does not exist, I need to move the uploaded file to the upload directory and perform the upload there.
Do you think the file upload process will be performed twice if the file does not exist?
Also, if the file already exists, we should delete it from the temporary file directory.
from multer.
Sorry for the late reply:
-
After the upload, I receive the file buffer
In fact, despite the downsides of using MemoryStorage, it'll work - if the file exists, you just discard the buffer; if it doesn't you can write the buffer to the disk to the relevant directory
-
upload process will be performed twice if the file does not exist
No, in this case the file will be uploaded and processed once - you upload it to MemoryStorage and then write it to the disk or discard
-
we should delete it from the temporary file directory
In this case you don't need a temp directory - you can save the buffer directly to the needed directory
from multer.
Related Issues (20)
- Issue with UTF-8 characters in filename HOT 1
- Issue with UTF-8 characters in filename HOT 1
- multer cannot handle images uploaded to cpanel hosting , the request is pending HOT 4
- How can I get other form keys in req.body before multer processing file? HOT 2
- Unexpected end of form on live env
- Second request hangs after upload fail into filter function
- can't upload multiple files
- I am also having the same error here, the error is saying "Cannot read properties of undefined (reading 'path')
- File delete issue HOT 2
- Multer & Express.js File Upload request hanged (never ending pending) HOT 3
- Distortion of the Russian file name HOT 4
- how can I upload image or json file ? HOT 1
- Why Postman filename is right, but in the web I use formdata.append() the filename is rong? HOT 1
- ERROR UPLOADING FILE
- ERROR UPLOADING FILE using the javascript and I also use multer in handling the fileupload HOT 5
- Translation of Documentation to Indonesian
- Multer gives error on serverless if using without express js
- Error in uploading a photo in javascript using a multer. HOT 2
- There is a way to effectively add a custom Error handler to multer? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from multer.