【问题标题】:ResumableUpload with google drive api v2 .netResumableUpload 与 google drive api v2 .net
【发布时间】:2012-10-27 22:16:25
【问题描述】:

我正在尝试将文件上传到谷歌驱动器并监控上传进度。我正在使用 .net 和 api v2。

我使用了 DriveService.Files.Insert 方法,该方法为我提供了 FilesResource.InsertMediaUpload,然后我使用 ProgressChanged 事件来列出进度。问题是这个事件只被调用了 3 次:“开始”、“上传”、“完成”状态。我想在“上传”状态下打很多电话。

这是我的代码:

    private UploadToGoogle(...)
    {
        //Consider I have an populated File and MemoryStream:
        MemoryStream contentStream = /*Populated memoryStream*/;
        File body = /*Populated file*/;

        FilesResource.InsertMediaUpload uploadRequest = null;

        try
        {
            contentStream.Position = 0;

            uploadRequest = driveService.Files.Insert(body, contentStream, body.MimeType);

            uploadRequest.ProgressChanged += new Action<Google.Apis.Upload.IUploadProgress>(UploadProgessEvent);

            Action asyncAction = () =>
            {
                uploadRequest.Upload();
                Console.WriteLine("Completed!");
            };

            var thread = new System.Threading.Thread(new System.Threading.ThreadStart(asyncAction));
            thread.Start();

        }catch(Exception) {}
    }

互联网上有一些关于旧 api 中的 ResumableUpload 类的内容,但不知道如何在 v2 中使用它。我在 Google.Apis.Upload 命名空间中找到了 ResumableUpload 类,但它是抽象的,我不知道如何使用它。

我在文档中没有发现任何关于进度监控的有用信息:https://developers.google.com/drive/manage-uploads

而且我真的不想通过 WebRequest 使用可恢复上传,有没有办法在 .net sdk 中实现它...

【问题讨论】:

    标签: c# asp.net-mvc upload google-drive-api


    【解决方案1】:

    ProgressChanged 事件仅在每次将完整的数据块发送到 API 时触发。如果您减小块大小或文件远大于默认块大小,您将看到该事件被多次触发。

    【讨论】:

    • 谢谢!是否可以在使用 DriveService.Files.Insert 方法完成上传之前获取文件 ID?
    • 文件id仅在上传完成后生成并返回
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    相关资源
    最近更新 更多