我决定在我的 WPF 项目中使用 Google drive api。我搜索了许多文档和示例。我学习并成功了。一切都很好。我使用此功能进行插入/上传。
public static Google.Apis.Drive.v2.Data.File InsertFile(DriveService service, String title, String description, String parentId, String mimeType, String filename)
{
Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = title;
body.Description = description;
body.MimeType = mimeType;
if (!String.IsNullOrEmpty(parentId))
{
body.Parents = new List<ParentReference>() { new ParentReference() { Id = parentId } };
}
byte[] byteArray = System.IO.File.ReadAllBytes(filename);
MemoryStream stream = new MemoryStream(byteArray);
try
{
FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);
request.Upload();
Google.Apis.Drive.v2.Data.File file = request.ResponseBody;
return file;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
}
}
当我想将一个小文件上传到谷歌驱动器时,它可以工作。但是我选择上传大文件,它给出了一个错误并且失败了。我收到了这个错误
System.Net.WebException was caught HResult=-2146233079 Message=The request was aborted: The request was canceled.Source=System StackTrace:
at System.Net.ConnectStream.InternalWrite(Boolean async, Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at Google.Apis.Upload.ResumableUpload`1.SendChunk(Stream stream, Uri uri, Int64 position)
at Google.Apis.Upload.ResumableUpload`1.Upload()
at Google.Apis.Util.Utilities.InsertFile(DriveService service, String title, String description, String parentId, String mimeType, String filename) in ..
我寻求这个错误并遇到同样的问题,但我不明白我错在哪里。任何人都可以帮助我或清楚地修复我的代码。谢谢 :)