c# - 使用 Google 文档 API

标签 c# php google-docs-api

我想知道如何将远程文档上传到 Google 文档,以及如何使用 Google 文档 API 使用 Google 文档查看该文档。

当我上传文件时,是否会返回一个 docid?这样我就可以在上传到我的帐户后使用它在线查看文档。

例如,我可以使用 docid 打开我帐户中的一个文档

https://docs.google.com/Doc?docid=0AdmWTLTOoWVBZGd3ZDdtZmhfMGZ3czNrNmho

这可以用 PHP 实现吗?我听说它不再支持。我更喜欢 PHP 而不是 C#,但如果不支持 PHP,我可以使用 C#。

最佳答案

您必须使用以下代码将文件下载到本地。

var request = new DocumentsRequest(settings);
request.BaseUri = "https://docs.google.com/feeds/default/private/full/folder" + folderResourceId + "/contents";
Feed<Document> feed = request.GetEverything();

foreach (Document entry in feed.Entries)
{
    if (entry.Title == fileName)
    {
        var fI = new FileInfo(uploadpath + entry.Title);
        Stream stream = request.Download(entry, "");
        arr = Read(stream);
        stream.Close();
        break;
     }
}

private Byte[] Read(Stream stream)
{
    //long originalPosition = stream.Position;
    //stream.Position = 0;

    try
    {
        Byte[] readBuffer = new Byte[4096];

        int totalBytesRead = 0;
        int bytesRead;

        while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
        {
            totalBytesRead += bytesRead;

            if (totalBytesRead == readBuffer.Length)
            {
                int nextByte = stream.ReadByte();
                if (nextByte != -1)
                {
                    Byte[] temp = new Byte[readBuffer.Length * 2];
                    Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                    Buffer.SetByte(temp, totalBytesRead, (Byte)nextByte);
                    readBuffer = temp;
                    totalBytesRead++;
                }
            }
        }

        Byte[] buffer = readBuffer;
        if (readBuffer.Length != totalBytesRead)
        {
            buffer = new Byte[totalBytesRead];
            Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
        }
        return buffer;
    }
    finally
    {
        //stream.Position = originalPosition;
    }
}

下载文件后保存到本地系统并使用asp.net显示

关于c# - 使用 Google 文档 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7752072/

相关文章:

c# - 在等待 filesystemwatcher 找到新更改的文件时,您可以让您的程序做其他事情吗?

c# - 如何使用两种不同形式的相同方法?

javascript - 是否可以在没有 ajax 的情况下在普通文件上传中显示进度条?

Java:Google Spreadsheet API 和上传 csv 文件?

java - 如何使用java-Google Docs Api将文件上传到Google Drive?

c# - 如何在 *.Designer.cs C# 中替换标签文本的硬编码字符串?

c# - 随机生成3x3x3 "block"大结构

php - 我如何使其面向对象?

php - <td> 修复文本过多时可扩展的宽度和高度

c# - 是否可以在未安装 MS Word 的情况下生成 .docx 文件?