C#从Google云端硬盘下载电子表格 [英] C# Downloading a spreadsheet from Google Drive

查看:86
本文介绍了C#从Google云端硬盘下载电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用FileResource API的ExportLinks从Google云端硬盘下载工作表.

I'm trying to download a sheet from Google Drive using the ExportLinks from FileResource API.

但是,我得到的响应不是电子表格,而是代表电子表格或类似名称的HTML文件.

However, the response I'm getting is not a spreadsheet but an HTML file representing the spreadsheet or something like that.

这是我正在使用的请求:

Here is the request I'm using:

            FilesResource.GetRequest getF = new FilesResource.GetRequest(service, "1y92Rok6oYKMwvc-Oq4Uurah7y552sfmIbyD9Wzmpq54");
            Google.Apis.Drive.v2.Data.File f = getF.Execute();
            string downloadUrl = f.ExportLinks["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"];
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(downloadUrl));
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            ReadWriteStream(response.GetResponseStream(), File.OpenWrite("D:\\tmp.xlsx"));

我用来存储流的函数:

static private void ReadWriteStream(Stream readStream, Stream writeStream)
        {
            int Length = 256;
            Byte[] buffer = new Byte[Length];
            int bytesRead = readStream.Read(buffer, 0, Length);
            // write the required bytes
            while (bytesRead > 0)
            {
                writeStream.Write(buffer, 0, bytesRead);
                bytesRead = readStream.Read(buffer, 0, Length);
            }
            readStream.Close();
            writeStream.Close();
        }

有人可以指出我在做什么错吗?

Can anyone point out what I'm doing wrong?

推荐答案

这是我通常使用的方法

/// <summary>
        /// Download a file
        /// Documentation: https://developers.google.com/drive/v2/reference/files/get
        /// </summary>
        /// <param name="_service">a Valid authenticated DriveService</param>
        /// <param name="_fileResource">File resource of the file to download</param>
        /// <param name="_saveTo">location of where to save the file including the file name to save it as.</param>
        /// <returns></returns>
        public static Boolean downloadFile(DriveService _service, File _fileResource, string _saveTo)
        {

            if (!String.IsNullOrEmpty(_fileResource.DownloadUrl))
            {
                try
                {
                    var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl );
                    byte[] arrBytes = x.Result;
                    System.IO.File.WriteAllBytes(_saveTo, arrBytes);
                    return true;                  
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: " + e.Message);
                    return false;
                }
            }
            else
            {
                // The file doesn't have any content stored on Drive.
                return false;
            }
        }

这篇关于C#从Google云端硬盘下载电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆