C# Google Drive Api v3 上传进度未启动

C# Google Drive Api v3 Uploadprogress not getting fire

这用于将文件上传到 drive.it,工作正常。 只有 IUploadProgress 没有触发,我看不到进度

namespace Google_Drive
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string filelocation = Server.MapPath(FileUpload1.PostedFile.FileName).Replace(@"\", @"\");

            string filename = FileUpload1.FileName;
            TextBox1.Text = filename;
            Stream fs = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            int size_ = (int)fs.Length;
            byte[] bytes = br.ReadBytes(size_);                

            Google.Apis.Drive.v3.Data.File asd = new Google.Apis.Drive.v3.Data.File();

            asd = upload(filelocation, filename, bytes, size_);
            Response.Write(filename + "__" + filelocation);
        }

        public Google.Apis.Drive.v3.Data.File upload(string _fileloc, string filename, byte[] bytes, int size_)
        {
            string[] Scopes = { DriveService.Scope.Drive };
            string ApplicationName = "Drive API .NET Quickstart";
            UserCredential credential;

            using (var stream =
                new FileStream(@"C:\client_secret.json", FileMode.Open, FileAccess.Read))
            {

                string credPath = "token.json";
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;                   
            }

            // Create Drive API service.
            var service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            var fileMetadata = new Google.Apis.Drive.v3.Data.File()
            {
                Name = filename
            };
            FilesResource.CreateMediaUpload request;
            System.IO.MemoryStream streama = new System.IO.MemoryStream(bytes);

            try
            {
                request = service.Files.Create(fileMetadata, streama, GetMimeType(filename));                   

                request.Fields = "*";
                request.Upload();
                request.ProgressChanged += Request_ProgressChanged1;

                return request.ResponseBody;

            }

            //  return request.ResponseBody;
            // var aaaa = request.ResponseBody;

            catch (Exception e)
            {
                HttpContext.Current.Response.Write("An error occurred: " + e.Message);
                return null;
            }
        }

        private void Request_ProgressChanged1(Google.Apis.Upload.IUploadProgress obj)
        {    
            switch (obj.Status)
            {    
                case UploadStatus.Uploading:
                {
                    Response.Write(obj.BytesSent.ToString());
                }
                break;
            }
        }

        private static string GetMimeType(string fileName)
        {
            string mimeType = "application/unknown";
            string ext = System.IO.Path.GetExtension(fileName).ToLower();
            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
            if (regKey != null && regKey.GetValue("Content Type") != null)
                mimeType = regKey.GetValue("Content Type").ToString();
            return mimeType;
        }
    }
}

我在这段代码中注意到的一件事是 request.Upload() 首先被调用 然后事件在随后的行中被订阅。

我建议尝试颠倒这两行并在调用 Upload() 之前先订阅事件。

我经常使用这个模式,它看起来很可靠。我能够在自己的代码中复制,如果在调用 Upload() 方法之后订阅了事件,那么它确实不会触发。