C# 使用c中的Dropbox Api读取大文件和块上传#
我有大量的sql备份,我想将它们保存在dropbox中,但我只想将副本发送到dropbox并将文件移动到外部硬盘,因为我的服务器硬盘空间有限 我正在尝试使用dropbox api的块上传,下面是他们给出的示例代码C# 使用c中的Dropbox Api读取大文件和块上传#,c#,api,dropbox,chunking,C#,Api,Dropbox,Chunking,我有大量的sql备份,我想将它们保存在dropbox中,但我只想将副本发送到dropbox并将文件移动到外部硬盘,因为我的服务器硬盘空间有限 我正在尝试使用dropbox api的块上传,下面是他们给出的示例代码 private async Task ChunkUpload(DropboxClient client, string folder, string fileName) { Console.WriteLine("Chunk upload file...");
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);
}
}
}
}
}
}
private async Task ChunkUpload(DropboxClient客户端、字符串文件夹、字符串文件名)
{
WriteLine(“区块上传文件…”);
//块大小为128KB。
常量int chunkSize=128*1024;
//创建一个1MB大小的随机文件。
var fileContent=新字节[1024*1024];
新的Random().NextBytes(fileContent);
使用(变量流=新内存流(文件内容))
{
int numChunks=(int)Math.天花((double)stream.Length/chunkSize);
字节[]缓冲区=新字节[chunkSize];
字符串sessionId=null;
对于(var idx=0;idx
我有文件路径,我想分块上传我的大文件,但就是无法让这个示例代码处理文件的发送路径。我对区块读取的修改不起作用,我在谷歌上搜索了两周。最后我想问一下这件事
我如何使用这个方法,发送文件路径,并从那个大文件上传一个块
从现在开始谢谢。示例使用随机字节 注释
random
流的代码,并使用语句将其包含在文件流中。在localContentFullPath
中包含带有文件名和扩展名的完整路径,并将文件名
分开,以便在DropBox中显示文件列表
public async Task ChunkUpload(DropboxClient client, string folder, string localContentFullPath)
{
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))
var filename = System.IO.Path.GetFileName(localContentFullPath.ToString());
using (var stream = new FileStream(localContentFullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
...
@Marlo,你能告诉我使用后我能用什么吗?文件夹指定dropbox文件夹名。这是我的密码。。使用(var stream=newfilestream(content,FileMode.Open,FileAccess.Read,FileShare.ReadWrite)){var updated=wait dbx.Files.UploadAsync(“/”+folder+“/”+file,WriteMode.Overwrite.Instance,body:stream);}