首页 > 解决方案 > Google drive api v3 c# Uploadprogress trigger during file Upload

问题描述

我正在使用 Drive api 上传文件。上传工作正常,进度也在触发。但在上传完成后触发。我希望在上传和响应网页时同时触发进度。试过这段代码。

        protected async void Button1_Click(object sender, EventArgs e)
        {
            await RunDownloadAsync();
        }

        private async Task RunDownloadAsync()
        {
            Google.Apis.Drive.v3.Data.File asd = new Google.Apis.Drive.v3.Data.File();
            asd = await Task.Run(() => upload(path, filename));    
        }

        public async Task<Google.Apis.Drive.v3.Data.File> upload(string _fileloc, string filename)
        {
           request = service.Files.Create(fileMetadata, stream, GetMimeType(_fileloc));
                //   request = service.Files.Create();

                request.Fields = "*";
                request.ChunkSize = 262144;
               request.ProgressChanged += (IUploadProgress)=> Request_ProgressChanged1(IUploadProgress);


                await Task.Run(()=> request.UploadAsync());
            }
            //  return request.ResponseBody;
            // var aaaa = request.ResponseBody;


            return request.ResponseBody;
        }


        private  void Request_ProgressChanged1(IUploadProgress obj)
        {
            double pc = (obj.BytesSent * 100) / FileUpload1.PostedFile.ContentLength;
            File.AppendAllText(@"d:\date.txt", DateTime.Now.ToString("hh:mm:ss.fff") + obj.Status.ToString() + " - " + obj.BytesSent + "____" + pc.ToString("0.00") + Environment.NewLine);
             Label2.Text += DateTime.Now.ToString("hh:mm:ss.fff") + obj.Status.ToString() + " - " + obj.BytesSent + "____" + pc + "<br>";
            Response.Write(DateTime.Now.ToString("hh:mm:ss.fff") + obj.Status.ToString() + " - " + obj.BytesSent + "____" + pc + "<br>");
            // close the stream

            //  HttpContext.Current.Response.Write(string.Format(obj.Status + " " + obj.BytesSent));
            //   HttpContext.Current.Response.Write(obj.BytesSent.ToString());

        }


    }
}

标签: c#apidrive

解决方案


推荐阅读