在C#中将文件上传到Rackspace云文件时跟踪进度

时间:2012-05-03 09:22:41

标签: c# .net rackspace-cloud cloudfiles

我正在使用https://github.com/rackspace/csharp-cloudfiles建立一个命令行工具,将文件上传到Rackspace Cloud Files。

问题是我不知道如何跟踪上传进度(似乎没有任何类型的事件或事情)。

以下是代码:

// Create credentials, client and connection
var creds = new UserCredentials(username, apiKey);
CF_Client client = new CF_Client();
Connection conn = new CF_Connection(creds, client);
conn.Authenticate();

// Get container and upload file
var container = new CF_Container(conn, client, containerName);
var obj = new CF_Object(conn, container, client, remoteFileName);
obj.WriteFromFile(localFilePath);

1 个答案:

答案 0 :(得分:1)

看起来没有一个内置的,不,但你可以添加自己的。

另一种方法是衡量投入;如果你看一下the source,你会发现WriteFromFile实际上只是

Dictionary<string,string> headers = new Dictionary<string,string>();
using(Stream stream = System.IO.File.OpenRead(localFilePath))
{
    obj.Write(stream, headers);
}

所以你可以将传递给你的流包装在另一个测量总字节读取进度的流类中(如果你搜索的话会有一些,或者它很容易自己编写)。如果您确实想要从代码中添加进度通知,则需要将其添加到已包装的OpenStack Client object中,但这也不应该太难。