gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:21:49 27 4
gpt4 key购买 nike

必须在 Dropbox 上上传大文件。我还想实现上传进度条。到处都提到我应该使用 UploadSessionStartAsync。我不知道如何使用 UploadSessionStartAsync 覆盖现有文件(当它已经存在时)。我可以先删除文件,然后进行新的上传,效果很好,但我不能这样做,因为先前文件的文件元数据会丢失。使用 UploadAsync 很容易,因为已经有一个 WriteMode.Overwrite 参数!这是我的代码:

/// <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);
}
}
}
}
}
}

最佳答案

the Dropbox API v2 .NET SDK 中使用上传 session 时,您可以设置WriteModeCommitInfo您传递给UploadSessionFinishAsync .

看起来像这样:

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

关于c# - 使用 DropBox SDK C# 使用 UploadSessionStartAsync 覆盖现有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51208762/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com