V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
flyzero
V2EX  ›  Flutter

写了个 webdav 客户端插件,支持常用文件操作

  •  
  •   flyzero · 2020-11-25 09:41:45 +08:00 · 2150 次点击
    这是一个创建于 1241 天前的主题,其中的信息可能已经有所发展或是发生改变。

    webdav_client

    ##主要功能


    Todo

    • Upload support for break uploads
    • Download support for breakdowns

    Usage

    First of all you should create client instance using newClient() function:

    var client = webdav.newClient(
        'http://localhost:6688/',
        user: 'flyzero',
        password: '123456',
        debug: true,
      );
    

    Common settings

        // Set the public request headers
        client.setHeaders({'accept-charset': 'utf-8'});
    
        // Set the connection server timeout time in milliseconds.
        client.setConnectTimeout(8000);
    
        // Set send data timeout time in milliseconds.
        client.setSendTimeout(8000);
    
        // Set transfer data time in milliseconds.
        client.setReceiveTimeout(8000);
    
        // Test whether the service can connect
        try {
          await client.ping();
        } catch (e) {
          print('$e');
        }
    

    Read all files in a folder

        var list = await client.readDir('/');
        list.forEach((f) {
            print('${f.name} ${f.path}');
          });
    
        // can sub folder
        var list2 = await client.readDir('/sub/sub/folder');
        list2.forEach((f) {
            print('${f.name} ${f.path}');
          });
        
    

    Create folder

        await client.mkdir('/新建文件夹');
    
        // Recursively create folders
        await client.mkdirAll('/new folder/new folder2');
    

    Remove a folder or file

    If you remove the folder, some webdav services require a '/' at the end of the path.

        // Delete folder
        await client.remove('/new folder/new folder2/');
    
        // Delete file
        await client.remove('/new folder/新建文本文档.txt');
    

    Rename a folder or file

    If you rename the folder, some webdav services require a '/' at the end of the path.

        // Rename folder
        await client.rename('/新建文件夹 /', '/新建文件夹 2/', true);
    
        // Rename file
        await client.rename('/新建文件夹 2/test.dart', '/新建文件夹 2/test2.dart', true);
    

    Copy a file / folder from A to B

    If copied the folder (A > B), it will copy all the contents of folder A to folder B.

    Some webdav services have been tested and found to delete the original contents of the B folder!!!

        // Copy all the contents of folderA to folder B
        await client.copy('/folder/folderA/', '/folder/folderB/', true);
    
        // Copy file
        await client.copy('/folder/aa.png', '/folder/bb.png', true);
    

    Download file

        // download bytes
        await client.read('/folder/folder/openvpn.exe');
    
        // download 2 local file
        await client.read2File(
              '/folder/vpn.exe', 'C:/Users/xxx/vpn2.exe');
    

    Upload file

        // upload local file 2 remote file
        await client.writeFromFile(
            'C:/Users/xxx/vpn.exe', '/f/vpn2.exe');
    

    Cancel request

        CancelToken cancel = CancelToken();
    
        // Supports most methods
        client.mkdir('/新建文件夹', cancel)
        .catchError((err) {
          prints(err.toString());
        });
    
        // in other
        cancel.cancel('reason')
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1211 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:09 · PVG 02:09 · LAX 11:09 · JFK 14:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.