Stop fs.createWriteStream creating a writable stream when deleting a file

People. I am creating an Angular / Node app where users upload files using the appropriate thumbnail.

  • When uploading files, a small list is displayed indicating the upload - using the status-bar .
  • When the file is uploaded, a success message is displayed.
  • Each list item has a delete button that deletes files when clicked. This all works great.

Question . Like this post - when the delete button is clicked the idea is to stop the download - which is why I think I'll just delete the file.

However, I use fs.createWriteStream

, and when the file is deleted, the stream seems to continue regardless of the fact that the file does not exist. This then brings up a status state file.on('finish', function() {

and displays a success message.

To solve this problem, I check if the file path exists when the state finish

starts up so that it correctly displays the success message. This seems to be quite a hack, especially when uploading large files.

Is there a way to cancel a stream from a process when deleting a file?


source to share

2 answers

After your comment "yes, just like that", I have one question. Obviously you are creating a file on the client system and writing to streams. How do you do it from the browser? Are you using any API that gives you access to the main node module in the browser? How to browserify .

Having said that, if my understanding is correct, you can achieve it like this

var http = require("http"),
        fs = require("fs"),
        stream = require("stream"),
        util = require("util"),
        abortStream=false,  // When user click on delete, update this flag to true

ws = fs.createWriteStream('./op.jpg');

// Transform streams  read input, process data [n times], output processed data
// readStream ---pipe---> transformStream1 ---pipe---> ...transformStreamn ---pipe---> outputStream
// @api
// @exmpl 
Transform = stream.Transform || require("readable-stream").Transform;

function InterruptedStream(options){
    if(!(this instanceof InterruptedStream)){
        return new InterruptedStream;
    }, options);

util.inherits(InterruptedStream, Transform);

InterruptedStream.prototype._transform = function (chunkdata, encoding, done) {
    // This is just for illustration, giving you the idea
    // Do not hard code the condition here.
    // Suggested to give the condition during constructor call, may be
        // Take care of this part.
        // Your logic might try to write in the stream after it is closed.
        // You can catch the exception but before that try not to write in the first place
        this.end(); // Stops the stream
    this.push(chunkdata, encoding);

var is=new InterruptedStream();

// Download large file
http.get("", function(res) {
        res.on('data', function(data) {
            // Simulates click on delete button
                // Delete the file, I think you have the logic in place
            }, 2000);
        }).on('end', function() {


The above code snippet gives you a general idea of ​​how to do this. You can just copy it, run it (it will work) and make changes.

If we are not on the same page please let me know, I am trying to correct my answer.



I think you can fire an event when your file is deleted and capture that event in

var wt = fs.createWriteStream();


this will close your writeableStream.

and the event deletion must be fired from the client side.



All Articles