Ejemplo n.º 1
0
        public string MoveFiles(String fileId, String folderId)
        {
            Google.Apis.Drive.v3.DriveService service = GetService();

            // Retrieve the existing parents to remove
            Google.Apis.Drive.v3.FilesResource.GetRequest getRequest = service.Files.Get(fileId);
            getRequest.Fields = "parents";
            Google.Apis.Drive.v3.Data.File file = getRequest.Execute();
            string previousParents = String.Join(",", file.Parents);

            // Move the file to the new folder
            Google.Apis.Drive.v3.FilesResource.UpdateRequest updateRequest = service.Files.Update(new Google.Apis.Drive.v3.Data.File(), fileId);
            updateRequest.Fields        = "id, parents";
            updateRequest.AddParents    = folderId;
            updateRequest.RemoveParents = previousParents;

            file = updateRequest.Execute();
            if (file != null)
            {
                return("Success");
            }
            else
            {
                return("Fail");
            }
        }
Ejemplo n.º 2
0
        //Download file from Google Drive by fileId.
        public static string DownloadGoogleFile(string fileId)
        {
            Google.Apis.Drive.v3.DriveService service = GetService();

            string FolderPath = System.Web.HttpContext.Current.Server.MapPath("/GoogleDriveFiles/");

            Google.Apis.Drive.v3.FilesResource.GetRequest request = service.Files.Get(fileId);

            string FileName = request.Execute().Name;
            string FilePath = System.IO.Path.Combine(FolderPath, FileName);

            MemoryStream stream1 = new MemoryStream();

            // Add a handler which will be notified on progress changes.
            // It will notify on each chunk download and when the
            // download is completed or failed.
            request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) =>
            {
                switch (progress.Status)
                {
                case DownloadStatus.Downloading:
                {
                    Console.WriteLine(progress.BytesDownloaded);
                    break;
                }

                case DownloadStatus.Completed:
                {
                    Console.WriteLine("Download complete.");
                    SaveStream(stream1, FilePath);
                    break;
                }

                case DownloadStatus.Failed:
                {
                    Console.WriteLine("Download failed.");
                    break;
                }
                }
            };
            request.Download(stream1);
            return(FilePath);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Realiza copy/paste de un archivo hacia otro directorio
        /// </summary>
        /// <param name="fileId"></param>
        /// <param name="folderId"></param>
        /// <returns></returns>
        public static string CopyFiles(String fileId, String folderId)
        {
            // Retrieve the existing parents to remove
            Google.Apis.Drive.v3.FilesResource.GetRequest getRequest = service.Files.Get(fileId);
            getRequest.Fields = "parents";
            Google.Apis.Drive.v3.Data.File file = getRequest.Execute();

            // Copy the file to the new folder
            Google.Apis.Drive.v3.FilesResource.UpdateRequest updateRequest = service.Files.Update(new Google.Apis.Drive.v3.Data.File(), fileId);
            updateRequest.Fields     = "id, parents";
            updateRequest.AddParents = folderId;
            file = updateRequest.Execute();
            if (file != null)
            {
                return("Success");
            }
            else
            {
                return("Fail");
            }
        }
Ejemplo n.º 4
0
        public async Task <int> MoveFile(string fileId, string FolderId)
        {
            Google.Apis.Drive.v3.FilesResource.GetRequest getRequest = service.Files.Get(fileId);
            getRequest.Fields = "parents";
            Google.Apis.Drive.v3.Data.File file = getRequest.Execute();
            string previousParents = string.Join(",", file.Parents);

            Google.Apis.Drive.v3.FilesResource.UpdateRequest updateRequest = service.Files.Update(new Google.Apis.Drive.v3.Data.File(), fileId);
            updateRequest.Fields        = "parents";
            updateRequest.AddParents    = FolderId;
            updateRequest.RemoveParents = previousParents;

            file = updateRequest.Execute();

            if (file != null)
            {
                return(1);
            }
            else
            {
                return(0);
            }
        }