【发布时间】:2016-05-21 10:46:45
【问题描述】:
我有大量的 sql 备份,我想将它们保存在保管箱中,但我只想将副本发送到保管箱并将文件移动到外部硬盘,因为我的服务器硬盘空间。
我正在尝试使用 dropbox api 的块上传,这是他们提供的示例代码。
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(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.UploadSessionAppendAsync(cursor, memStream);
}
}
}
}
}
}
我有文件路径,我想分块上传我的大文件,但无法让这个示例代码与发送文件路径一起工作。我对块读取的修改不起作用,我在谷歌上搜索了 2 周。最后我想问一下这个。
我怎样才能使这个方法与发送文件路径一起工作,并从那个大文件上传一个块?
从现在开始谢谢。
【问题讨论】: