How to upload/update file by FileStream and ResumableUploader in C#

牧云@^-^@ 提交于 2019-12-04 06:34:32

问题


I wanna upload/update file by System.IO.FileStream in Google Documents List API(C#)?

I use two way below: 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) Success.
(2) Failed with 403 Forbidden or another...

So, does someone have any sample code about (2)?

My Code for (2): This code is edited by sample code from Claudio Cherubino, and it's work for uploading file stream to Google Documents(Drive). But the file(DocumentEntry)'s name show 'Untitled' on page of Google Drive. It seems 'slug' not work. Do I miss some important settings?

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()
);

回答1:


Try the following code:

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());


来源:https://stackoverflow.com/questions/10962335/how-to-upload-update-file-by-filestream-and-resumableuploader-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!