0

I want to download document from Google Drive.for that I am using Google Drive API. I am very new to Google Drive API so can any one tell me how to download document from google Drive ?

I have try this option from here but I am getting exception on line

HttpWebRequest request = (HttpWebRequest)WebRequest.Create( new Uri(file.DownloadUrl.ToString()));

authenticator.ApplyAuthenticationToRequest(request); HttpWebResponse response = (HttpWebResponse)request.GetResponse();

like

The underlying connection was closed: An unexpected error occurred on a send.

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

I thing this problem occur because of scope. please tell me how to set scope in application

pls not I am creating desktop application in c# .net

can any one help me ?

4

3 回答 3

4

这段代码对我有用:

        var stream = service.HttpClient.GetStreamAsync(file.DownloadUrl);
        var result = stream.Result;
        using (var fileStream = System.IO.File.Create(filePathToSaveFileOnDisk))
        {
            result.CopyTo(fileStream);
        }
于 2013-08-29T16:55:52.867 回答
0

我正在使用此代码从谷歌驱动器下载文件

public void Download(object sender, DoWorkEventArgs e)
        {
            List<File> driveFiles = Google.Apis.Util.Utilities.RetrieveAllFiles(service);
            int fileCount = driveFiles.Count;
            int i = 0;
            IAuthenticator authenticator = new CloudManager().CreateAuthentication();
            foreach (var driveFile in driveFiles.Where(driveFile => driveFile.MimeType != "video/mp4"))
            {
                LabelFileProcess.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                                        (Action) (() => LabelFileProcess.Content = driveFile.Title));
                string title = driveFile.Title;
                LabelFileProcess.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                        (Action) (() => LabelFileProcess.Content = title));
                if (string.IsNullOrEmpty(driveFile.Title))
                {
                    MessageBox.Show(@"File's title is emplty");
                    continue;
                }

                if (driveFile.MimeType != "application/vnd.google-apps.folder")
                {

                    Stream stream = Utilities.DownloadFile(authenticator, driveFile);
                    if (stream != null)
                        Utilities.SaveFile(stream, driveFile.Title);
                }
                else
                    Directory.CreateDirectory("D:\\GdriveFiles\\" + driveFile.Title);
            }

主要下载处理方式

public void Download(object sender, DoWorkEventArgs e)
        {
            List<File> driveFiles = Google.Apis.Util.Utilities.RetrieveAllFiles(service);
            int fileCount = driveFiles.Count;
            int i = 0;
            IAuthenticator authenticator = new CloudManager().CreateAuthentication();
            foreach (var driveFile in driveFiles.Where(driveFile => driveFile.MimeType != "video/mp4"))
            {
                LabelFileProcess.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                                        (Action) (() => LabelFileProcess.Content = driveFile.Title));
                string title = driveFile.Title;
                LabelFileProcess.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                        (Action) (() => LabelFileProcess.Content = title));
                if (string.IsNullOrEmpty(driveFile.Title))
                {
                    MessageBox.Show(@"File's title is emplty");
                    continue;
                }

                if (driveFile.MimeType != "application/vnd.google-apps.folder")
                {

                    Stream stream = DownloadFile(authenticator, driveFile);
                    if (stream != null)
                        SaveFile(stream, driveFile.Title);
                }
                else
                    Directory.CreateDirectory("D:\\GdriveFiles\\" + driveFile.Title);
            }


public static System.IO.Stream DownloadFile(IAuthenticator authenticator, File file)
        {
            if (!string.IsNullOrEmpty(file.DownloadUrl))
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(file.DownloadUrl));
                    authenticator.ApplyAuthenticationToRequest(request);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    return response.StatusCode == HttpStatusCode.OK ? response.GetResponseStream() : null;
                }
                catch (Exception e)
                {
                    System.Windows.Forms.MessageBox.Show("Exception occures " + e.Message);
                }
            else
                System.Windows.Forms.MessageBox.Show(@"File doesn't have any content on Drive, Title: "+file.Title);
            return null;
        }

        public static void SaveFile(System.IO.Stream stream, String title)
        {
            StreamReader streamReader = new StreamReader(stream);
            //System.Windows.MessageBox.Show(streamReader.ToString());
            if (stream == null)
                System.Windows.MessageBox.Show("Error Occured during download");
            else
            {


                FileStream fileStream = System.IO.File.Create("D:\\GdriveFiles\\" + title);
                char[] charArray = new char[100];
                int count;// = streamReader.Read(arrayByte, 0, 100);
                //streamReader.Read(arrayByte, 0, (int)stream.Length);
                //fileStream.Write(arrayByte,0,arrayByte.Length);
                string incomingMessage = "";
                do
                {
                    try
                    {
                        count = streamReader.Read(charArray, 0, 100);
                        incomingMessage += new string(charArray, 0, count);
                        byte[] byteArray = new byte[charArray.Length];
                        //byteArray = System.Text.Encoding.Unicode.GetBytes(charArray);
                        byteArray = System.Text.Encoding.ASCII.GetBytes(charArray);
                        fileStream.Write(byteArray, 0, count);
                    }
                    catch (ArgumentException ex)
                    {
                        MessageBox.Show(@"File ended, Exception:" + ex.Message);
                        break;
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Exception occure" + e.Message);
                        break;
                    }

                } while (count > 0);
                fileStream.Close();
                //MessageBox.Show("File Contains are " + incomingMessage);
            }
        }
于 2013-08-22T12:12:51.663 回答
0

看看这里的代码示例:https ://developers.google.com/drive/v2/reference/files/get

于 2013-07-12T08:21:11.373 回答