文件上传Dropbox v2.0 API [英] File uploading Dropbox v2.0 API

查看:180
本文介绍了文件上传Dropbox v2.0 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用适用于.NET的新Dropbox SDK v2。

I'm using the new Dropbox SDK v2 for .NET.

我正在尝试将文档上传到Dropbox帐户。

I'm trying to upload a document to a Dropbox account.

public async Task UploadDoc()
    {
        using (var dbx = new DropboxClient("XXXXXXXXXX"))
        {
            var full = await dbx.Users.GetCurrentAccountAsync();
            await Upload(dbx, @"/MyApp/test", "test.txt","Testing!");
        }
    }
async Task Upload(DropboxClient dbx, string folder, string file, string content)
    {
        using (var mem = new MemoryStream(Encoding.UTF8.GetBytes(content)))
        {
            var updated = await dbx.Files.UploadAsync(
                folder + "/" + file,
                WriteMode.Overwrite.Instance,
                body: mem);

            Console.WriteLine("Saved {0}/{1} rev {2}", folder, file, updated.Rev);
        }
    }

此代码段实际上在以下位置创建了一个test.txt文档带有测试中!的Dropbox帐户内容,但我想上传具有给定路径文档(例如: C:\MyDocuments\test.txt),这可能吗?

This code snippet actually creates a test.txt document on the Dropbox account with the "Testing!" content, but I want to upload a document, with a given path (for example: "C:\MyDocuments\test.txt"), is that possible?

任何帮助将不胜感激。

推荐答案

a href = https://dropbox.github.io/dropbox-sdk-dotnet/html/M_Dropbox_Api_Files_Routes_FilesUserRoutes_UploadAsync_1.htm rel = noreferrer> UploadAsync 方法将使用传递给 body 参数的所有数据作为上载文件内容。

The UploadAsync method will use whatever data you pass to the body parameter as the uploaded file content.

如果要上传本地文件的内容,则需要为其提供该文件流。

If you want to upload the contents of a local file, you'll need to give it a stream for that file.

这里有一个示例,显示了如何使用此方法上传本地文件(包括处理大文件的逻辑):

There's an example here that shows how to use this method to upload a local file (including logic for handling large files):

此示例使用<一个href = https://github.com/dropbox/dropbox-sdk-dotnet rel = noreferrer> Dropbox .NET库,使用较大文件的上传会话将文件上传到Dropbox帐户:

This example uses the Dropbox .NET library to upload a file to a Dropbox account, using upload sessions for larger files:

private async Task Upload(string localPath, string remotePath)
{
    const int ChunkSize = 4096 * 1024;
    using (var fileStream = File.Open(localPath, FileMode.Open))
    {
        if (fileStream.Length <= ChunkSize)
        {
            await this.client.Files.UploadAsync(remotePath, body: fileStream);
        }
        else
        {
            await this.ChunkUpload(remotePath, fileStream, (int)ChunkSize);
        }
    }
}

private async Task ChunkUpload(String path, FileStream stream, int chunkSize)
{
    ulong numChunks = (ulong)Math.Ceiling((double)stream.Length / chunkSize);
    byte[] buffer = new byte[chunkSize];
    string sessionId = null;
    for (ulong idx = 0; idx < numChunks; idx++)
    {
        var byteRead = stream.Read(buffer, 0, chunkSize);

        using (var memStream = new MemoryStream(buffer, 0, byteRead))
        {
            if (idx == 0)
            {
                var result = await this.client.Files.UploadSessionStartAsync(false, memStream);
                sessionId = result.SessionId;
            }
            else
            {
                var cursor = new UploadSessionCursor(sessionId, (ulong)chunkSize * idx);

                if (idx == numChunks - 1)
                {
                    FileMetadata fileMetadata = await this.client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(path), memStream);
                    Console.WriteLine (fileMetadata.PathDisplay);
                }
                else
                {
                    await this.client.Files.UploadSessionAppendV2Async(cursor, false, memStream);
                }
            }
        }
    }
}

这篇关于文件上传Dropbox v2.0 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆