在 UWP 平台上使用 Dropbox.NET 下载 PNG 文件到本地机器

Download PNG file to local machine using Dropbox.NET on UWP platform

我可以将 PNG 图像上传到 Dropbox 文件夹,但是我不知道如何从 Dropbox 下载 PNG(或其他图像)。我从教程页面得到的是:

async Task Download(DropboxClient dbx, string folder, string file)
{
    using (var response = await dbx.Files.DownloadAsync(folder + "/" + file))
    {
        Console.WriteLine(await response.GetContentAsStringAsync());
    }
}

谁有下载文件到本地驱动器的示例代码?谢谢

Dropbox API .NET SDK DownloadAsync method gives you a IDownloadResponse提供了三种获取文件内容的方法:

  • GetContentAsByteArrayAsync
  • GetContentAsStreamAsync
  • GetContentAsStringAsync

例如,要将文件内容保存到本地文件,您可以这样做:

public async Task Download(string remoteFilePath, string localFilePath)
{
    using (var response = await client.Files.DownloadAsync(remoteFilePath))
    {
        using (var fileStream = File.Create(localFilePath))
        {
            response.GetContentAsStreamAsync().Result.CopyTo(fileStream);
        }

    }
}

这会将文件内容从远程 Dropbox 文件路径 remoteFilePath 的文件下载到本地路径 localFilePath

经过一些发现和尝试,我终于找到了解决方案:

public static async Task Download(string folder, string file)
{
    StorageFolder storeFolder = ApplicationData.Current.LocalFolder;
    CreationCollisionOption options = CreationCollisionOption.ReplaceExisting;
    StorageFile outputFile = await storeFolder.CreateFileAsync("temp.png", options);

    using (var dbx = new DropboxClient(yourAccessToken))
    {
          var response = await dbx.Files.DownloadAsync(downloadFolder);
          {
               using (var file = await outputFile.OpenStreamForWriteAsync())
               {
                    Stream imageStream = await response.GetContentAsStreamAsync();
                    CopyStream(imageStream, file);
               }
          }
     }
}

有辅助函数:

public static void CopyStream(Stream input, Stream output)
{
    byte[] buffer = new byte[8 * 1024];
    int len;
    while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, len);
    }
}

要上传文件:

public static async Task Upload(string filename, string filePath)
{
    try
    {
        string TargetPath = "/data/" + filename + ".png";
        const int ChunkSize = 4096 * 1024;
        using (var dbx = new DropboxClient(yourAccessToken))
        {
            using (var fileStream = File.Open(filePath, FileMode.Open))
            {
                if (fileStream.Length <= ChunkSize)
                {
                    await dbx.Files.UploadAsync(TargetPath, null, false, null, false, body: fileStream);
                }
                else
                {
                    MessageDialog dialog = new MessageDialog("File is too big");
                    await dialog.ShowAsync();
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageDialog dialog = new MessageDialog("Error uploading file. " + ex.Message);
        await dialog.ShowAsync();
    }
}

希望对您有所帮助。谢谢

@Greg 说的对。我想对代码中提到的 localFilePath 做一个小改动,也应该有一个扩展名。例如,它应该是 C:\Code\image.jgp 而不是 C:\Code。如果指定的文件位置不存在,它将自动创建,此代码将完美运行。

public async Task Download(string remoteFilePath, string localFilePath)
{
    using (var response = await client.Files.DownloadAsync(remoteFilePath))
    {
      using (var fileStream = File.Create(localFilePath))
      {
         response.GetContentAsStreamAsync().Result.CopyTo(fileStream);
      }
    }
}

这是我使用 Dropbox.Net 下载文件的逻辑 API:

private async Task Download(DropboxClient dbx, string remoteFilePath, string localFilePath) {
  using(var response = await dbx.Files.DownloadAsync(remoteFilePath)) {
    var fileSize = response.Response.Size;
    var bufferSize = 1024 * 1024;
    var buffer = new byte[bufferSize];

    using(var stream = await response.GetContentAsStreamAsync()) {
      using(System.IO.FileStream file = new System.IO.FileStream(localFilePath, FileMode.OpenOrCreate)) {
        var length = stream.Read(buffer, 0, bufferSize);
        while (length > 0) {
          file.Write(buffer, 0, length);
          var percentage = 100 * file.Length / (double) fileSize;
          Console.WriteLine(percentage);
          length = stream.Read(buffer, 0, bufferSize);
        }
      }
    }
  }
}

你可以这样称呼它:

Await(Download(dbx, url, @"yourDestinationFolder\" + item.Name));

其中 item.Name 是下载文件的全名,例如setup.exe