Загрузите изображение или медиафайл на Google Диск с помощью C# в MVC
Я хочу загрузить изображение на google Диск с помощью c# в mvc.
Что я уже пробовал:
My code is <pre> string[] scopes = new string[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile}; var clientId = "clientid"; var clientSecret = "client Secret Key"; 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 = "application Name", }); uploadFile(service, "D:\\img\\adv2.jpeg", "", "photos");
public static void uploadFile(DriveService _service, string _uploadFile, string _parent, string _descrp = "Uploaded with .NET!") { 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 } }; byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile); System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray); FilesResource.CreateMediaUpload request; try { //FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile)); //request.Upload(); //return request.ResponseBody; using (var stream1 = new System.IO.FileStream(_uploadFile, System.IO.FileMode.Open)) { request = _service.Files.Create(body, stream1, GetMimeType(_uploadFile)); request.Fields = "id"; request.Upload(); } var file = request.ResponseBody; var fili = file.Id; } catch (Exception e) { //MessageBox.Show(e.Message, "Error Occured"); } } else { //MessageBox.Show("The file does not exist.", "404"); } } 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; }
Я получаю null в "файле"
var file = request.ResponseBody;.
пожалуйста, подскажите мне, почему файл не загружается..