Dropbox v2.0 API上传文件

时间:2015-11-15 02:12:29

标签: c# winforms

所以我需要使用c#

将文件上传到我的保管箱帐户

我目前正在使用相对较新的Dropbox API v2.0。

我正在使用winforms c#,我在这里给了Dropbox这个示例代码:https://www.dropbox.com/developers/documentation/dotnet#tutorial

我需要一些帮助,我不太明白如何在winforms中调用上传。

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        var task = Task.Run((Func<Task>)this.Upload);
        task.Wait();
    }

    async Task Upload()
    {
        DropboxClient dbx = new DropboxClient("FF5tO7fs20AAAAAAAAALcjFNxDeEpWSneNOPl6ya9276gFB7s7pUJKVI2vAxFcSs");
        var file = "159386425737-IPs.txt";
        var mem = new MemoryStream(Encoding.UTF8.GetBytes("TESTING" + IPCount.ToString()));
        try
        {
            var updated = await dbx.Files.UploadAsync(file, WriteMode.Overwrite.Instance, body: mem);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }

我得到的错误是在这一行

var updated = await.....

这是它正在抛出的运行时错误

System.ArgumentsOutOfRangeException: Specified argument was out of range of valid values.
Parameter name: Path

和此错误

Dropbox.Api.BadInputException : Error in call to API function "files/upload" : HTTP header "Dropbox-Api-Arg": path: "folder//file"" did not match parretn '/.*'

1 个答案:

答案 0 :(得分:0)

由于函数声明为async并返回表示异步方法调用的任务,因此您只需确保在让表单关闭之前等待Task完成。试试这个......

Delay for claiming slot 0 is 0
Delay for claiming slot 1 is 0
Received message with sequence 0. EndOfBatch = true
Received message with sequence 1. EndOfBatch = true
Delay for claiming slot 2 is 0
Received message with sequence 2. EndOfBatch = true
Delay for claiming slot 3 is 0
Received message with sequence 3. EndOfBatch = true
Delay for claiming slot 4 is 0
Received message with sequence 4. EndOfBatch = true
Delay for claiming slot 5 is 0
Received message with sequence 5. EndOfBatch = true
Delay for claiming slot 6 is 0
Received message with sequence 6. EndOfBatch = true
Delay for claiming slot 7 is 0
Received message with sequence 7. EndOfBatch = true
Delay for claiming slot 8 is 1
Delay for claiming slot 9 is 0
Received message with sequence 8. EndOfBatch = false
Received message with sequence 9. EndOfBatch = true