如何使用C#在Google驱动器上上传文件

时间:2019-08-05 19:06:49

标签: c# c#-4.0 google-api google-drive-api

我在Google驱动器上有以下代码上传文件,当我上传.Csv文件时,其工作正常。使用 MimeType = vnd.google-apps.spreadsheet ,但是当我使用 MimeType = vnd.google-apps.file 上传其他文件File.bak时,它不起作用 下图显示错误。任何人都知道如何解决此错误

  

记住:csv文件240k,但是当我上传File.bak的32MB

  UserCredential credential;

                    using (var stream =
                        new FileStream("credentials.json", FileMode.Open, FileAccess.ReadWrite))
                    {
                        // The file token.json stores the user's access and refresh tokens, and is created
                        // automatically when the authorization flow completes for the first time.
                        string credPath = "token.json";
                        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                            GoogleClientSecrets.Load(stream).Secrets,
                            Scopes,
                            "user",
                            CancellationToken.None,
                            new FileDataStore(credPath, true)).Result;
                        Console.WriteLine("Credential file saved to: " + credPath);
                    }

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

                    // Define parameters of request.
                    FilesResource.ListRequest listRequest = service.Files.List();
                    listRequest.PageSize = 10;
                    listRequest.Fields = "nextPageToken, files(id, name)";

                    // List files.
                    IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
                        .Files;
                    Console.WriteLine("Files:");             
                    var fileMetadata = new Google.Apis.Drive.v3.Data.File()
                    {
                        Name = "HUCMS-"+DateTime.Now.ToString("yyyy-MM-dd"),
                        MimeType = "application/vnd.google-apps.file"
                    };

                    var request = service.Files.Create(fileMetadata);
                    request.Fields = "id";
                    var file = request.Execute();           
                    string folderID = file.Id;

                    UploadImage(backupFileName, service, folderID);
                    Console.ReadKey();
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    Console.WriteLine(ex.StackTrace.ToString());
                    Console.ReadKey();
                }


       private static void UploadImage(string path,DriveService service, string folderupload)
            {
                var fileMetaData = new Google.Apis.Drive.v3.Data.File();
                fileMetaData.Name = Path.GetFileName(path);
                fileMetaData.MimeType = "file/*";
                fileMetaData.Parents = new List<string>
                {
                    folderupload
                };
                FilesResource.CreateMediaUpload request;
                using (var stream = new System.IO.FileStream(path,
                                        System.IO.FileMode.Open))
                {
                    request = service.Files.Create(
                        fileMetaData, stream, "file/*");
                    request.Fields = "id";
                    request.Upload();
                }
                Console.WriteLine(request);
                var filess = request.ResponseBody;
            }

enter image description here

0 个答案:

没有答案