c# - C# Google Drive Api v3 Uploadprogress 没有起火
问题描述
这是用于将文件上传到驱动器。它工作正常。只有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;
}
}
}
解决方案
One thing I notice in this code is that request.Upload() is called first and then the event is subscribed in the line that follows.
I would recommend trying to reverse those two lines and subscribe to the event first before calling Upload().
I use this pattern a lot and it seems pretty reliable. I was able to replicate in my own code that if the event is subscribed after the call to the Upload() method then, indeed, it doesn't fire.
推荐阅读
- javascript - 将内容放入变量
- java - Java - 检查一个字符串是否是另一个字符串的子集
- firebase - 在颤振上注册后显示名称返回 null
- linux - 在 Linux 上使用 New-Item 创建一个新目录?
- ios - 使用 RxSwift 的 API 函数
- assembly - 如何正确输入字符串并输出?
- javascript - 使用 if 和 else 检查 for 循环中的所有值
- angular - 在ngrx效果中嵌套管道需要什么?
- c# - 无法使用 List 手动创建 Lambda 表达式树
- python - Django ORM:如何对一个值进行分组并获取该组中最后一个元素的不同值