节点-FTP-获取并重新启动

发布于 2025-02-11 02:06:31 字数 1500 浏览 3 评论 0原文

我创建了一个效果很好的FTP应用程序。 我可以上传和下载。我显示进度。 我可以恢复上传,但是... 我有一个恢复下载的问题。

我使用此代码重新启动下载。

// When Connection Ready
clientFtp.on('ready', function(){

    // Source and Destination determination
    var serverFile  = "example_file_on_server.zip";
    var localFile   = "example_file_on_client.zip";

    // List Remote File
    clientFtp.list(serverFile,function(erreurListage, serverElementList) {

        // Get Total File Weight
      serverFileSize = serverElementList[0].size;

        // Get Local File Info
        localFileInfo   = fs.statSync(localFile);
        let restartSize = localFileInfo.size;

        // Forcing for Restart from...
        clientFtp.restart(restartSize,function(erreurRedemarrage){

            // Initialization
            tailleDownload = restartSize;

            // Get File
            clientFtp.get(serverFile, function(erreurRecuperation, stream) {


            stream.pipe(fs.createWriteStream(localFile, {flag:'r+',start: restartSize}));

            stream.on('data',function(buffer){

                        tailleDownload          = buffer.length+tailleDownload;
                        pourcentageDownload =   (tailleDownload/serverFileSize*100).toFixed(0);

                        console.log(pourcentageDownload);
        });

            

            });
        });
    });         
});

我必须指定用于读取下载流的代码在不重新启动的情况下效果很好。 但是,当我将其与RESTART一起使用时,我有一个尺寸良好但不可读取的文件(这是zip文件)。该文件似乎损坏了。

注意:我使用R+标志并重新启动,但我使用它们正确...?

I created an FTP application that works well.
I can upload and download. I display the progression.
I can resume a upload but...
I have an issue to resume a download.

I use this code to restart a download.

// When Connection Ready
clientFtp.on('ready', function(){

    // Source and Destination determination
    var serverFile  = "example_file_on_server.zip";
    var localFile   = "example_file_on_client.zip";

    // List Remote File
    clientFtp.list(serverFile,function(erreurListage, serverElementList) {

        // Get Total File Weight
      serverFileSize = serverElementList[0].size;

        // Get Local File Info
        localFileInfo   = fs.statSync(localFile);
        let restartSize = localFileInfo.size;

        // Forcing for Restart from...
        clientFtp.restart(restartSize,function(erreurRedemarrage){

            // Initialization
            tailleDownload = restartSize;

            // Get File
            clientFtp.get(serverFile, function(erreurRecuperation, stream) {


            stream.pipe(fs.createWriteStream(localFile, {flag:'r+',start: restartSize}));

            stream.on('data',function(buffer){

                        tailleDownload          = buffer.length+tailleDownload;
                        pourcentageDownload =   (tailleDownload/serverFileSize*100).toFixed(0);

                        console.log(pourcentageDownload);
        });

            

            });
        });
    });         
});

I must specify that the code used for reading the download stream works well without restarting.
But when I use it with restart, I have a file with a good size but not readable (it's a zip file). The file seems corrupted.

Note : I use r+ flag and restart but I use them correcty...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡莣 2025-02-18 02:06:31

我回答我的问题,然后开始:rtfm !!!
为什么...?因为它是 flags ,而不是

我继续使用此信息。
不要在createWritestream中使用启动。
如果您在FTP连接上使用重新启动,则无需在CreateWritestream中放置启动选项。

此代码工作正常

stream.pipe(fs.CreateWriteStream(localfile,{flags:'a'}));>

I answer to my question and I begin with : RTFM !!!
Why...? Because it's flags for options and not flag

I continue with this information.
Don't use start in createWriteStream.
If you use restart on FTP connection, you don't need to put start option in createWriteStream.

This code works fine

stream.pipe(fs.createWriteStream(localFile,{flags: 'a'}));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文