下载文件错误:无法转换类型

时间:2018-02-18 05:45:37

标签: c# .net dropbox dropbox-api

我试图从我的DropBox帐户下载文件。

我收到了一个错误 var task = Task.Run((Func<Task>)Download("", "largetest.mpk", folderName));

错误:
Cannot convert type 'System.Threading.Tasks.Task' to 'System.Func<System.Threading.Tasks.Task>

private void button1_Click(object sender, EventArgs e)
 {
 string folderName = @"c:\dropboxTest\test.exe";
var task = Task.Run((Func<Task>)Download("", "largetest.mpk", folderName));
task.Wait();
}
async Task Download(string folder, string targetfile, string localPath)
    {
        var dbx = new DropboxClient(Form1.api);
        var response = await dbx.Files.DownloadAsync(folder + "/" + targetfile);
        ulong fileSize = response.Response.Size;
        const int bufferSize = 1024 * 1024;
        var buffer = new byte[bufferSize];
        string folderName = @"C:\dropboxTest\teasdfst.exe";
        using (var stream = await response.GetContentAsStreamAsync())
        {
            using (var localfile = new FileStream(folderName, FileMode.OpenOrCreate))
            {
                var length = stream.Read(buffer, 0, bufferSize);
                while (length > 0)
                {
                    localfile.Write(buffer, 0, length);
                   // Console.WriteLine(localfile.);
                    var percentage = 100 * (ulong)localfile.Length / fileSize;
                    // Update progress bar with the percentage.
                    // progressBar.Value = (int)percentage
                    //Console.WriteLine(percentage);
                    length = stream.Read(buffer, 0, bufferSize);
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

你应该像这样编写你的函数

void Download(string folder, string targetfile, string localPath)
{
    var dbx = new DropboxClient(Form1.api);
    var response = dbx.Files.Download(folder + "/" + targetfile);
    ulong fileSize = response.Response.Size;
    const int bufferSize = 1024 * 1024;
    var buffer = new byte[bufferSize];
    string folderName = @"C:\dropboxTest\teasdfst.exe";
    using (var stream = response.GetContentAsStream())
    {
        using (var localfile = new FileStream(folderName, FileMode.OpenOrCreate))
        {
            var length = stream.Read(buffer, 0, bufferSize);
            while (length > 0)
            {
                localfile.Write(buffer, 0, length);
               // Console.WriteLine(localfile.);
                var percentage = 100 * (ulong)localfile.Length / fileSize;
                // Update progress bar with the percentage.
                // progressBar.Value = (int)percentage
                //Console.WriteLine(percentage);
                length = stream.Read(buffer, 0, bufferSize);
            }
        }
    }
}

然后拨打Task.Run,如下所示

private void button1_Click(object sender, EventArgs e)
{
    string folderName = @"c:\dropboxTest\test.exe";
    var task = Task.Run(() => Download("", "largetest.mpk", folderName));
    task.Wait(); // remove this if you don't want to block UI thread.
}

希望这会有所帮助!!

答案 1 :(得分:1)

你不需要做所有这些。没有转化,没有演员表,没有Task.Run

只需写下

const string folderName = @"c:\dropboxTest\test";

private async void button1_Click(object sender, EventArgs e)
{
    await Download("", "largetest.mpk", folderName);
}

请注意,我们直接等待方法调用。请注意如何删除对Task.Wait的调用。

请注意,除了,如果是事件处理程序,需要void方法,async方法应该是return Task个对象。这非常重要,特别是对于正确的异常处理。这是async void适当的唯一情况。

您的Download方法应该按惯例重命名为DownloadAsync,但已经有正确的返回类型Task,并且不应该在这方面进行更改。