gpt4 book ai didi

javascript - 如何知道文件是否下载完成?

转载 作者:行者123 更新时间:2023-11-30 19:21:51 25 4
gpt4 key购买 nike

我正在使用 electron 为 mac 和 windows 开发一个应用程序,该应用程序监视一个文件夹以查找从另一个应用程序下载到那里的文件,我想在将文件移动到其他地方之前检查文件是否完成下载,我不知道是否应该我定期检查文件大小,或者是否有另一种方法可以知道文件是否使用 javascript 完成下载。

最佳答案

您可以使用fs.watch@ronomon/opened 来解决您的任务。参见:Check if a file is open in another process

我写了一个简单的脚本,在我的 Windows 10 机器上运行良好。我将一些文件下载到文件夹中,并在下载结束时打印出正确的通知。

const fs = require ('fs');
const path = require ("path")
const opened = require('@ronomon/opened');

const base_path = "."
fs.watch (base_path, {encoding: "ascii", recursive: true}, (eventType, filename)=> {

var file_path = path.join (base_path, filename);
opened.files ([file_path], (error, hashtable) => {
try {
if (!hashtable[file_path]) {
console.log ("file is closed and can be further processed", file_path)
} else {
console.log ("file is still opend by any other process...", file_path)
}
} catch (e) {
console.error ("error: ", e);
}
})
});

关于javascript - 如何知道文件是否下载完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57366018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com