代码之家  ›  专栏  ›  技术社区  ›  Morteza Jangjoo

如何使用MVC中的C在Google Drive中上载MP3文件

  •  1
  • Morteza Jangjoo  · 技术社区  · 6 年前

    我已经成功地用c在google驱动器中上传了图像文件。但我在上传MP3文件时遇到了问题。

    如何使用MVC中的C?在Google Drive中上载MP3文件?

    这是我保存文件的代码:

    public ServieResponse SaveFileOnGoogleDrive(string url)
           {
               //string url = string.Empty; ; ;
               //string[] scopes = new string[] { DriveService.Scope.Drive,
               //                 DriveService.Scope.DriveFile};
               string[] scopes = new string[] { DriveService.Scope.Drive,
                              DriveService.Scope.DriveAppdata,
                              //DriveService.Scope.DriveAppsReadonly,
                              DriveService.Scope.DriveFile,
                              DriveService.Scope.DriveMetadataReadonly,
                              DriveService.Scope.DriveReadonly,
                              DriveService.Scope.DriveScripts };
               var clientId = "xxxxxx";      // From https://console.developers.google.com
               var clientSecret = "xxxxxxx";
    
               // From https://console.developers.google.com
               // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
               var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
               {
                   ClientId = clientId,
                   ClientSecret = clientSecret
               },
                                                                       scopes,
                                                                       Environment.UserName,
                                                                       CancellationToken.None,
                                                                       new FileDataStore("MyAppsToken")).Result;
               //Once consent is recieved, your token will be stored locally on the AppData directory, so that next time you wont be prompted for consent.
    
               DriveService service = new DriveService(new BaseClientService.Initializer()
               {
                   HttpClientInitializer = credential,
                   ApplicationName = "CTM",
               });
    
               string filePath = WebConfigurationManager.AppSettings["filPath"];
               filePath = HttpContext.Current.Server.MapPath(filePath) + url;
               uploadFile(service, filePath, "", "");
    
               // service.HttpClient.Timeout = TimeSpan.FromMinutes(100);
               //Long Operations like file uploads might timeout. 100 is just precautionary value, can be set to any reasonable value depending on what you use your service for.
    
               //return Json(new { result = "abc" }, JsonRequestBehavior.AllowGet);
               ServieResponse ob = new ServieResponse();
               ob.ResponseMsg = "Success";
               return ob;
           }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Alavi    6 年前

    现在我的代码运行良好。更新的代码是:

    public ServieResponse SaveFileOnGoogleDrive(string url)
       {
           //string url = string.Empty; ; ;
           //string[] scopes = new string[] { DriveService.Scope.Drive,
           //                 DriveService.Scope.DriveFile};
           string[] scopes = new string[] { DriveService.Scope.Drive,
                          DriveService.Scope.DriveAppdata,
                          //DriveService.Scope.DriveAppsReadonly,
                          DriveService.Scope.DriveFile,
                          DriveService.Scope.DriveMetadataReadonly,
                          DriveService.Scope.DriveReadonly,
                          DriveService.Scope.DriveScripts };
           var clientId = "65675933715-poet7f7dhjhrmccmalhb41pltho7tusr.apps.googleusercontent.com";      // From https://console.developers.google.com
           var clientSecret = "D8USyz3Pf82wOMi6l2pJehjx";
    
           // From https://console.developers.google.com
           // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
           var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
           {
               ClientId = clientId,
               ClientSecret = clientSecret
           },
                                                                   scopes,
                                                                   Environment.UserName,
                                                                   CancellationToken.None,
                                                                   new FileDataStore("MyAppsToken")).Result;
           //Once consent is recieved, your token will be stored locally on the AppData directory, so that next time you wont be prompted for consent.
    
           DriveService service = new DriveService(new BaseClientService.Initializer()
           {
               HttpClientInitializer = credential,
               ApplicationName = "CTM",
           });
    
           string filePath = WebConfigurationManager.AppSettings["filPath"];
           filePath = HttpContext.Current.Server.MapPath(filePath) + url;
           uploadFile(service, filePath);
           ServieResponse ob = new ServieResponse();
           ob.ResponseMsg = "Success";
           return ob;
       }
    
    public static void uploadFile(DriveService _service, string _uploadFile)
      {
          if (System.IO.File.Exists(_uploadFile))
          {
              var body = new Google.Apis.Drive.v3.Data.File();
              //File body = new File();
              body.Name = System.IO.Path.GetFileName(_uploadFile);
              //body.Description = _descrp;
              body.MimeType = GetMimeType(_uploadFile);
              // body.Parents = new List<ParentReference>() { new ParentReference() { Id = _parent } };
    
              FilesResource.CreateMediaUpload request;
              try
              {
                  using (var stream = new System.IO.FileStream(_uploadFile, System.IO.FileMode.Open))
                  {
                      request = _service.Files.Create(body, stream, body.MimeType);
                      request.Fields = "id";
                      request.Upload();
                  }
                  var file = request.ResponseBody;
                  var fili = file.Id;
              }
              catch (Exception e)
              {
    
              }
          }
          else
          {
    
          }
      }
    
    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;
      }