Javascript 删除文件时停止fs.createWriteStream创建可写流
伙计们:我正在创建一个Angular/Node应用程序,用户可以通过选择相关的缩略图下载文件Javascript 删除文件时停止fs.createWriteStream创建可写流,javascript,node.js,Javascript,Node.js,伙计们:我正在创建一个Angular/Node应用程序,用户可以通过选择相关的缩略图下载文件 在下载文件时,会显示一个小列表,其中包含下载进度-使用 下载文件时,将显示一条成功消息 列表中的每个项目都有一个删除按钮,单击该按钮可删除文件。所有这些都很好 问题:类似于-当单击删除按钮时,想法是停止下载-这就是为什么我认为我应该删除文件 但是,我使用的是fs.createWriteStream,当文件被删除时,流似乎会继续,而不管文件是否存在。这将导致文件.on('finish',functio
- 在下载文件时,会显示一个小列表,其中包含下载进度-使用李>
- 下载文件时,将显示一条成功消息李>
- 列表中的每个项目都有一个删除按钮,单击该按钮可删除文件。所有这些都很好
fs.createWriteStream
,当文件被删除时,流似乎会继续,而不管文件是否存在。这将导致文件.on('finish',function(){
状态启动并显示成功消息
为了解决这个问题,我检查了当finish
状态启动时文件路径是否存在,以便正确显示成功消息。这感觉非常糟糕,尤其是当有大文件下载时
当文件被删除时,有没有办法取消流的进程?根据您的评论“是的,就像那样”,我有一个问题。您显然是在客户端系统中创建文件,并在流中写入。您是如何从浏览器中执行此操作的?您是否使用任何API来访问浏览器中节点的核心模块?Li柯 话虽如此,如果我的理解是正确的,你可以通过以下方式实现
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,
Transform;
ws = fs.createWriteStream('./op.jpg');
// Transform streams read input, process data [n times], output processed data
// readStream ---pipe---> transformStream1 ---pipe---> ...transformStreamn ---pipe---> outputStream
// @api https://nodejs.org/api/stream.html#stream_class_stream_transform
// @exmpl https://strongloop.com/strongblog/practical-examples-of-the-new-node-js-streams-api/
Transform = stream.Transform || require("readable-stream").Transform;
function InterruptedStream(options){
if(!(this instanceof InterruptedStream)){
return new InterruptedStream;
}
Transform.call(this, 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
if(abortStream===true){
// 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);
done();
};
var is=new InterruptedStream();
is.pipe(ws);
// Download large file
http.get("http://www.zastavki.com/pictures/1920x1200/2011/Space_Huge_explosion_031412_.jpg", function(res) {
res.on('data', function(data) {
is.write(data);
// Simulates click on delete button
setTimeout(function(){
abortStream=false;
res.destroy();
// Delete the file, I think you have the logic in place
}, 2000);
}).on('end', function() {
console.log("end");
});
});
上面的代码片段给出了如何完成它的大致想法。您可以复制粘贴它,运行(它会工作)并进行更改
如果我们不在同一页上,请让我知道,我将尝试更正我的答案。我认为当您的文件被删除时,您可以发出一个事件,并在中捕获该事件
var wt = fs.createWriteStream();
wt.on('eventName',function(){
wt.emit('close');
})
这将关闭您的writableStream
并且应该从客户端触发删除事件。我想你可以调用file.end(optionalCallback);@Molda-似乎不起作用。有没有办法破坏流?你是通过get方法请求下载的吗?我的意思是,你是通过这样做来创建写流吗?
http.get(options,function(res)){res.on('data',function(data){file.write(data);}).on('end',function(){file.end();});
@years_of_no_light-是的,就像那样-有什么想法吗?