【问题标题】:How to upload/update file by FileStream and ResumableUploader in C#如何在 C# 中通过 FileStream 和 ResumableUploader 上传/更新文件
【发布时间】:2012-06-09 15:43:15
【问题描述】:

我想通过 Google Documents List API(C#) 中的 System.IO.FileStream 上传/更新文件?

我使用以下两种方式:Google.GData.Client.ResumableUpload.ResumableUploader
(1) public void UpdateAsync(Authenticator authentication, AbstractEntry payload, object userData)
(2) public void UpdateAsync(Authenticator authentication, Uri resumableUploadUri, Stream payload, string contentType, object userData)

(1) 成功。
(2) 因 403 Forbidden 或其他原因失败...

那么,有人有关于(2)的示例代码吗?

我的 (2) 代码: 此代码由 Claudio Cherubino 的示例代码编辑,用于将文件流上传到 Google 文档(驱动器)。但是文件(DocumentEntry)的名称在 Google Drive 的页面上显示为“Untitled”。似乎“蛞蝓”不起作用。我错过了一些重要的设置吗?

Google.GData.Documents.DocumentsService conn =
new Google.GData.Documents.DocumentsService("TestSend");
conn.setUserCredentials("UserName", "UserPass");

string path = @"D:\test_file\test.exe"; 

Google.GData.Client.ResumableUpload.ResumableUploader send =
new Google.GData.Client.ResumableUpload.ResumableUploader();
send.AsyncOperationCompleted += new Google.GData.Client.AsyncOperationCompletedEventHandler(
    delegate(object sender,
        Google.GData.Client.AsyncOperationCompletedEventArgs e)
    {
        System.Windows.Forms.MessageBox.Show("File Send Done");
    }
);

System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);

send.InsertAsync(
    new Google.GData.Client.ClientLoginAuthenticator("TestSend", "writely", this._DiskConn.Credentials),
    new System.Uri("https://docs.google.com/feeds/upload/create-session/default/private/full?v=3"),
    fs,
    "application/octet-stream",
    System.IO.Path.GetFileName(path),
    new object()
);

【问题讨论】:

    标签: c# filestream gdata google-docs-api google-drive-api


    【解决方案1】:

    试试下面的代码:

    DocumentsService service = new DocumentsService("MyDocumentsListIntegration-v1");
    ClientLoginAuthenticator authenticator = new ClientLoginAuthenticator(APPLICATION_NAME, ServiceNames.Documents, USERNAME, PASSWORD);
    
    string slug = "Legal contract";
    MediaFileSource mediaSource = new MediaFileSource("c:\\contract.txt", "text/plain");
    Uri createUploadUrl = new Uri("https://docs.google.com/feeds/upload/create-session/default/private/full?v=3");
    
    ResumableUploader ru = new ResumableUploader();
    ru.InsertAsync(authenticator, createUploadUrl, mediaSource.GetDataStream(), mediaSource.ContentType, slug, new object());
    

    【讨论】:

    • 成功了!我将 'mediaSource.GetDataStream()' 替换为 'new FileStream("[file path]", FileMode.Open)',并在 Google Drive 上找到了该文件。但有点奇怪,Google Drive 中名为“Untitled”的 DocumentEntry,slug 无法正常工作。我用我的新代码上传我的帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 2012-04-04
    相关资源
    最近更新 更多