【问题标题】:Issue in Download file from google drive api in .net从 .net 中的 google drive api 下载文件中的问题
【发布时间】:2013-08-30 00:55:52
【问题描述】:

我想从 Google Drive 下载文档。因为我正在使用 Google Drive API。我对 Google Drive API 很陌生,谁能告诉我如何从 Google Drive 下载文档?

我已经从here 尝试过这个选项,但我在网上遇到了异常

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

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

喜欢

底层连接已关闭:发生意外错误 发送。

无法从传输连接读取数据:现有 连接被远程主机强行关闭。

我认为这个问题是由于范围而发生的。请告诉我如何在应用程序中设置范围

请不要我在 c# .net 中创建桌面应用程序

谁能帮帮我?

【问题讨论】:

  • 您的套接字似乎因某些网络问题而断开连接。由于范围错误,问题不会发生,我们会以 403 响应,并带有您未授权的人类可读错误消息。可以包含堆栈跟踪吗?
  • stacktrace like : at System.Net.HttpWebRequest.GetResponse() at GoogleDriveSamples.DriveCommandLineSample.DownloadFile(IAuthenticator authenticationator, File file) in D:\Misc\RandDwork\Google Drive\google-drive-v2 -rev82-csharp-1.4.0-beta\drive\GoogleDriveApplication\GoogleDriveApplication\Program.cs:line 80 and InnerException like: {“无法从传输连接读取数据:现有连接被远程主机强行关闭。” }
  • 感谢重播但每次都给出相同的异常,所以我认为网络问题不存在,请注意我能够上传文件,也能够获取元数据但无法下载文件跨度>
  • 知道了,正在调查是否是 Google 问题。前几天有人报告了类似的问题,可能是部分用户的问题,我们无法轻易识别。
  • 你能建议我如何从 google drive API 下载文件。

标签: c# .net google-drive-api


【解决方案1】:

此代码适用于我:

        var stream = service.HttpClient.GetStreamAsync(file.DownloadUrl);
        var result = stream.Result;
        using (var fileStream = System.IO.File.Create(filePathToSaveFileOnDisk))
        {
            result.CopyTo(fileStream);
        }

【讨论】:

    【解决方案2】:

    【讨论】:

    • 我使用了相同的代码。但我得到了我在代码中提到的异常。并在请求对象初始化后,“Connection = null”的状态
    【解决方案3】:

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

    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);
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多