【问题标题】:Using the Google Docs API使用 Google 文档 API
【发布时间】:2011-10-13 09:28:51
【问题描述】:

我想知道如何将远程文档上传到 Google Docs,并能够通过 Google Docs API 使用 Google Docs 查看该文档。

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

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

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

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

【问题讨论】:

  • 你真的看过文档吗?
  • 我有,但我不明白。请多多包涵,因为我是编程新手

标签: c# php google-docs-api


【解决方案1】:

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

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显示

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多