简体   繁体   English

使用DropBox SDK C#使用UploadSessionStartAsync覆盖现有文件

[英]Overwrite existing file with UploadSessionStartAsync with DropBox SDK C#

Have to upload large files on Dropbox. 必须在Dropbox上上传大文件。 I want to implement also and progress bar for upload. 我也想实施进度条并上传。 Everywhere it is mentioned that I should use UploadSessionStartAsync. 到处都提到我应该使用UploadSessionStartAsync。 What I do not know is how to overwrite existing file (when it already exists) with UploadSessionStartAsync. 我不知道如何使用UploadSessionStartAsync覆盖现有文件(如果已经存在)。 I could first delete file and then do a new upload and that works fine, but I can not do that as filemetadata of previous file is then lost. 我可以先删除文件,然后进行新的上载,但效果很好,但是由于先前文件的filemetadata随后丢失,因此无法执行。 With UploadAsync it is easy as there is already a WriteMode.Overwrite argument! 使用UploadAsync很容易,因为已经有一个WriteMode.Overwrite参数! Here is my code: 这是我的代码:

/// <summary>
        /// Uploads a big file in chunk. The is very helpful for uploading large file in slow network condition
        /// and also enable capability to track upload progerss.
        /// </summary>
        /// <param name="client">The Dropbox client.</param>
        /// <param name="folder">The folder to upload the file.</param>
        /// <param name="fileName">The name of the file.</param>
        /// <returns></returns>
        private async Task ChunkUpload(DropboxClient client, string folder, string fileName)
        {
            Console.WriteLine("Chunk upload file...");
            // Chunk size is 128KB.
            const int chunkSize = 128 * 1024;

            // Create a random file of 1MB in size.
            var fileContent = new byte[1024 * 1024];
            new Random().NextBytes(fileContent);

            using (var stream = new MemoryStream(fileContent))
            {
                int numChunks = (int)Math.Ceiling((double)stream.Length / chunkSize);

                byte[] buffer = new byte[chunkSize];
                string sessionId = null;

                for (var idx = 0; idx < numChunks; idx++)
                {
                    Console.WriteLine("Start uploading chunk {0}", idx);
                    var byteRead = stream.Read(buffer, 0, chunkSize);

                    using (MemoryStream memStream = new MemoryStream(buffer, 0, byteRead))
                    {
                        if (idx == 0)
                        {
                            var result = await client.Files.UploadSessionStartAsync( body: memStream);
                            sessionId = result.SessionId;
                        }

                        else
                        {
                            UploadSessionCursor cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));

                            if (idx == numChunks - 1)
                            {
                                await client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(folder + "/" + fileName), memStream);
                            }

                            else
                            {
                                await client.Files.UploadSessionAppendV2Async(cursor, body: memStream);
                            }
                        }
                    }
                }
            }
        }

When using upload sessions in the Dropbox API v2 .NET SDK , you can set the WriteMode in the CommitInfo that you pass to UploadSessionFinishAsync . 当使用上传会话Dropbox的API V2 .NET SDK ,可以设置WriteModeCommitInfo你传递给UploadSessionFinishAsync

That would look like this: 看起来像这样:

await client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(folder + "/" + fileName, mode:WriteMode.Overwrite.Instance), memStream);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM