C# 在UWP平台上使用Dropbox.NET将PNG文件下载到本地计算机
我可以上传PNG图像到Dropbox文件夹,但我不知道如何从Dropbox下载PNG(或其他图像)。我从教程页面得到的信息是:C# 在UWP平台上使用Dropbox.NET将PNG文件下载到本地计算机,c#,dropbox-api,windows-10-universal,C#,Dropbox Api,Windows 10 Universal,我可以上传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 r
async Task Download(DropboxClient dbx, string folder, string file)
{
using (var response = await dbx.Files.DownloadAsync(folder + "/" + file))
{
Console.WriteLine(await response.GetContentAsStringAsync());
}
}
有人有将文件下载到本地驱动器的示例代码吗?谢谢。为您提供了获取文件内容的三种方法:
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();
}
}
公共静态异步任务上载(字符串文件名、字符串文件路径)
{
尝试
{
字符串TargetPath=“/data/”+filename+”.png”;
常量int ChunkSize=4096*1024;
使用(var dbx=新的DropboxClient(yourAccessToken))
{
使用(var fileStream=File.Open(filePath,FileMode.Open))
{
如果(fileStream.Length@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感谢Greg的帮助:)您发布的解决方案让我了解了如何在我的项目中实现它。这对我很有用…不要忘记使用System.IO;
的,谢谢它工作得很好。请您上传一个示例代码,以便在Dropbox上上载文件