예제 #1
0
        // Upload file to the Google drive
        public static string UploadImage(string fileName, String imageLocation)
        {
            DriveService service = GetService();

            try
            {
                // Initial validation.
                if (service == null)
                {
                    throw new ArgumentNullException("service");
                }

                string folder = "1WGiO9s1F6gtjbuGRsRf7htkoV9PsN0x0";

                var request = service.Files.List();
                request.Fields = "files(id, name)";
                var resultado = request.Execute();
                var arquivos  = resultado.Files;

                if (arquivos != null && arquivos.Any())
                {
                    foreach (var arquivo in arquivos)
                    {
                        if (service.Files.Get(arquivo.Id).Execute().Name == fileName)
                        {
                            GoogleDriveFiles file = new GoogleDriveFiles();
                            file.Id = arquivo.Id;
                            DeleteFile(file);
                        }
                    }
                }

                Stream image = new FileStream(imageLocation, FileMode.Open, FileAccess.Read);


                File fileMetadata = new File();
                fileMetadata.Name    = fileName;
                fileMetadata.Parents = new List <string>()
                {
                    folder
                };

                service.Files.Create(fileMetadata, image, "").Upload();
                return("200");
            }
            catch (Exception)
            {
                return("500");
            }
        }