【问题标题】:Upload zipped file to Dropbox using C#使用 C# 将压缩文件上传到 Dropbox
【发布时间】:2017-02-07 06:57:22
【问题描述】:

我正在尝试使用访问令牌将压缩文件上传到 Dropbox。以下代码适用于解压缩文件

private static async Task FileUploadToDropbox(string filePath, string fileName, byte[] fileContent)
{
    var client = new DropboxClient("Access Token");

    const int chunkSize = 1024;

    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++)
        {
            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(filePath + "/" + fileName), memStream);
                    }

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

但是当我尝试使用此代码上传压缩文件时,它会将一个空的压缩文件上传到 Dropbox。我正在将压缩文件作为字节数组读取并将其传递给上述方法。虽然文件大小保持不变,但当我下载文件并尝试解压缩时,它说压缩文件是空的。

【问题讨论】:

    标签: c# dropbox-api


    【解决方案1】:

    请试试这个:

        /// <summary>
        /// Function to import local file to dropbox.
        /// </summary>
        public static async Task<bool> WriteFileToDropBox()
        {
            try
            {
                //Connecting with dropbox.
                var file = "File path at dropbox";
                using (var dbx = new DropboxClient("Access Token"))
                using (var fs = new FileStream("Path of file to be uploaded.")
                {
                    var updated = await dbx.Files.UploadAsync(file, WriteMode.Add.Instance, body: fs);
                }
                return true;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return false;
            }
        }
    

    【讨论】:

    • @Akshay,如果您觉得有帮助,请将其标记为答案,以便对其他人也有帮助。
    • 我收到一个异常:“流不支持阅读。”使用您的代码。
    【解决方案2】:
    private static async Task FileUploadToDropbox(string filePath, string fileName, string fileSource)
            {
                using (var dbx = new DropboxClient("access Token"))
                using (var fs = new FileStream(fileSource, FileMode.Open, FileAccess.Read))
                {
                    var updated = await dbx.Files.UploadAsync(
                        (filePath + "/" + fileName), WriteMode.Overwrite.Instance, body: fs);
                }
            }
    

    以上方法对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多