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.

file is not removed if upload is canceled

See original GitHub issue

Consider this minimal working example (Multer v1.1.0 on Windows 7, OS X 10.11 and Ubuntu 14.04 with node v4.2.1):

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

app.post("/upload", upload.single("file"), function(req, res, next){
    res.end("uploaded to: " + req.file.filename);
});
app.listen(80);

and the following test script

# create large test file
$ dd if=/dev/urandom bs=2G count=1 of=upload
# upload
$ curl -F "file=@upload" http://localhost/upload
# upload again, but cancel before upload is finished
timeout -s SIGKILL 2s curl -F "file=@upload" http://localhost/upload

Expected behavior: Only the first file is persisted, the second is removed after the upload got canceled.

Actual behaviour: Both files are stored in the uploads folder

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:6
  • Comments:40 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
itsvaibhav10commented, Sep 14, 2020

Here is my approach worked for me

filename: (req, file, cb) => {
    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
    const fileName =
      file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname);
    cb(null, fileName);    
    req.on('aborted', () => {
      const fullFilePath = path.join('uploads', 'videos', fileName);
      file.stream.on('end', () => {
        fs.unlink(fullFilePath, (err) => {
          console.log(fullFilePath);
          if (err) {
            throw err;
          }
        });
      });
      file.stream.emit('end');
    })
  }
6reactions
ghostcommented, Aug 31, 2016

I thought perhaps the error handling code from the README was the solution, but it seems the callback is not called if the request is aborted.

This post might help.

EDIT : This is what I came up with :

const upload = multer({dest: __dirname + '/../../resource/'}).single('file')
app.post('/upload', (req, res) => {

    req.on('close', () => {
        console.error('req aborted by client')
        // delete most recent file ?
    })

    upload(req, res, () => {
        // do normal stuff
    })
})

We know when if the upload gets canceled. However the partially uploaded file still can’t be deleted, since only multer knows its name ; deleting the most recent file could be a temporary solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multer file is not removed if upload is canceled - Stack Overflow
Is there Any Function in Multer To Remove uploaded part of the File from the server if The Request gets Cancelled.
Read more >
Cancel button does not delete the uploaded file after ... - Telerik
Hi Konstantinos, The Cancel button is used to cancel the current upload session. After the file has been uploaded, the session is complete...
Read more >
Delete uploaded data if browser closed or cancel upload
It seems that if browser is closed, some of the data is already stored locally. And if you meet error, The program has...
Read more >
How to detect when cancel is clicked on file input using ...
javascript · When the user uploads any file, the length of the files is found using theFile.value.length property. · When the user does...
Read more >
431098 - Impossible to clear file upload controls (fields)
In FF2 days, I'd simply select the file path in the text box, and delete it. ... I.e. it cancels the whole action...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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