C# 使用googledocsapi

C# 使用googledocsapi,c#,php,google-docs-api,C#,Php,Google Docs Api,我想知道如何将远程文档上传到Google Docs,并能够使用Google Docs API使用Google Docs查看该文档 当我上传文件时,是否会用docid响应?这样我可以在上传到我的帐户后使用它在线查看文档 例如,我可以使用docid打开我帐户中的一个文档 这可以在PHP中实现吗?我听说它不再受支持了。我更喜欢PHP而不是C#,但是如果不支持PHP,我可以使用C#。您必须使用以下代码在本地下载文件 var request = new DocumentsRequest(settings

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

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

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


这可以在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;
    }
}
var请求=新文档请求(设置);
request.BaseUri=”https://docs.google.com/feeds/default/private/full/folder“+folderResourceId+”/contents”;
Feed=request.GetEverything();
foreach(feed.Entries中的文档条目)
{
if(entry.Title==文件名)
{
var fI=新文件信息(uploadpath+entry.Title);
Stream=request.Download(条目“”);
arr=读取(流);
stream.Close();
打破
}
}
私有字节[]读取(流)
{
//长原始位置=流位置;
//流位置=0;
尝试
{
字节[]读缓冲区=新字节[4096];
int totalBytesRead=0;
int字节读取;
而((bytesRead=stream.Read(readBuffer,totalBytesRead,readBuffer.Length-totalBytesRead))>0)
{
totalBytesRead+=字节读取;
if(totalBytesRead==readBuffer.Length)
{
int nextByte=stream.ReadByte();
如果(下一字节!=-1)
{
字节[]临时=新字节[readBuffer.Length*2];
块复制(readBuffer,0,temp,0,readBuffer.Length);
Buffer.SetByte(临时,totalBytesRead,(字节)nextByte);
readBuffer=temp;
totalBytesRead++;
}
}
}
字节[]缓冲区=读取缓冲区;
if(readBuffer.Length!=totalBytesRead)
{
缓冲区=新字节[totalBytesRead];
块复制(readBuffer,0,Buffer,0,totalBytesRead);
}
返回缓冲区;
}
最后
{
//流位置=原始位置;
}
}

下载文件后,将其保存到本地系统并使用asp.net显示,您必须使用以下代码下载本地文件

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;
    }
}
var请求=新文档请求(设置);
request.BaseUri=”https://docs.google.com/feeds/default/private/full/folder“+folderResourceId+”/contents”;
Feed=request.GetEverything();
foreach(feed.Entries中的文档条目)
{
if(entry.Title==文件名)
{
var fI=新文件信息(uploadpath+entry.Title);
Stream=request.Download(条目“”);
arr=读取(流);
stream.Close();
打破
}
}
私有字节[]读取(流)
{
//长原始位置=流位置;
//流位置=0;
尝试
{
字节[]读缓冲区=新字节[4096];
int totalBytesRead=0;
int字节读取;
而((bytesRead=stream.Read(readBuffer,totalBytesRead,readBuffer.Length-totalBytesRead))>0)
{
totalBytesRead+=字节读取;
if(totalBytesRead==readBuffer.Length)
{
int nextByte=stream.ReadByte();
如果(下一字节!=-1)
{
字节[]临时=新字节[readBuffer.Length*2];
块复制(readBuffer,0,temp,0,readBuffer.Length);
Buffer.SetByte(临时,totalBytesRead,(字节)nextByte);
readBuffer=temp;
totalBytesRead++;
}
}
}
字节[]缓冲区=读取缓冲区;
if(readBuffer.Length!=totalBytesRead)
{
缓冲区=新字节[totalBytesRead];
块复制(readBuffer,0,Buffer,0,totalBytesRead);
}
返回缓冲区;
}
最后
{
//流位置=原始位置;
}
}

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

您是否确实看过文档?我看过,但我不理解。请容忍我,因为我是新来编程的。你真的看过文档了吗?我看过,但我不懂。请容忍我,因为我是编程新手