[C#Google云端硬盘Api v3 Uploadprogress无法正常启动

问题描述 投票:0回答:1

这是用于将文件上传到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;
        }
    }
}
c# api drive
1个回答
0
投票

我在这段代码中注意到的一件事是request.Upload()被称为first,然后在下一行中订阅了该事件。

我建议尝试反转这两行,并在调用Upload()之前先订阅该事件。

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

© www.soinside.com 2019 - 2024. All rights reserved.