【问题标题】:flutter connect FTP and download file颤振连接FTP并下载文件
【发布时间】:2021-12-27 06:46:57
【问题描述】:

我正在尝试使用ftpconnect 1.0.0 连接到 FTP 并下载文件。
有什么不明白的,所以写了一个问题。
如果你看一下连接 FTP 并下载文件的方法,它需要两个参数。

/// Download the Remote File [sRemoteName] to the local File [fFile]
Future<bool> downloadFile(
    String? sRemoteName,
    File fFile, {
    TransferMode mode = TransferMode.binary,
    FileProgress? onProgress,
    bool? supportIPv6,
  }) {
    return FileDownload(_socket, mode, _log).downloadFile(sRemoteName, fFile,
        onProgress: onProgress, supportIPV6: supportIPv6);
  }

sRemoteName 是 FTP 上的文件名,fFile 是您要保存的文件吗?
如果正确的话,如何设置保存在应用中的路径?
提前感谢您的回复。

我的源代码

import 'dart:io';

import 'package:flutter/widgets.dart';
import 'package:ftpconnect/ftpconnect.dart';

class FTPServer extends ChangeNotifier{
  FTPConnect? ftpConnect;
  File? file;

  Future<void> _fileMock(String strFileName) async {
    final Directory directory = Directory('/test')
      ..createSync(recursive: true);
    file = File('${directory.path}/$strFileName');
    notifyListeners();
  }

  //FTP Connection
  connectionFTP() async {
    ftpConnect = FTPConnect('IP address', user: 'user', pass: 'password', port: portNumber);
    notifyListeners();
  }

  //Change Dir
  Future<void> changeDirectoryFTP(String strFilePath) async {
    try {
      bool bRes = await ftpConnect!.connect();
      await ftpConnect!.changeDirectory(strFilePath);
    }catch(e){
      print('Error : ${e.toString()}');
    }
    notifyListeners();
  }

  //Download File
  Future<void> downloadFileFTP() async{
    try {
      bool bRes = await ftpConnect!.connect();
      await ftpConnect!.downloadFileWithRetry('FTP File Name', File name to save , pRetryCount: 1);
      await ftpConnect!.disconnect();
    }catch(e){
      print('Error : ${e.toString()}');
    }
    notifyListeners();
  }

}

【问题讨论】:

    标签: flutter dart ftp


    【解决方案1】:

    我找到了解决办法!
    我使用path_provider 将文件保存到适当的路径。下面是修改后的源码。

    
      Future<void> _fileMock(String strFileName) async {
        final Directory appDocDir  = await getApplicationDocumentsDirectory();
        String appDocPath = appDocDir.path;
        file = File('$appDocPath/$strFileName');
        print('appDocPath : $appDocPath');
        print('file : $file');
        notifyListeners();
      }
    
      //connect FTP
      connectionFTP() async {
        ftpConnect = FTPConnect('ip addr', user: 'user', pass: 'pass', port: port);
        notifyListeners();
      }
    
    //download file
      Future<void> downloadFileFTP() async{
        try {
          bool bRes = await ftpConnect!.connect();
          _fileMock('filename');
          await ftpConnect!.downloadFileWithRetry('ftp_filename', file!, pRetryCount: 1);
          print('path2 : ${file!.path}');
          await ftpConnect!.disconnect();
    
          print('file Name : ${file!}');
        }catch(e){
          print('Error : ${e.toString()}');
        }
        notifyListeners();
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-10
      • 2022-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      • 2020-04-24
      相关资源
      最近更新 更多