如何使用Google Drive API和C#将文件上传到Google云端硬盘

时间:2014-01-13 04:36:12

标签: c# asp.net google-drive-api

我想使用Google Drive API从我的ASP.NET项目上传文件。

如何在ASP.NET中使用DriveService

我搜索了很多,但我没有得到DriveService的正确结果。

1 个答案:

答案 0 :(得分:1)

尝试https://developers.google.com/drive/quickstart-cshttp://code.google.com/p/google-api-dotnet-client/

来自https://developers.google.com/drive/v2/reference/files/insert#examples: -

using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Requests;
using System.Collections.Generic;
using System.Net;

// ...

public class MyClass {

  // ...

  /// <summary>
  /// Insert new file.
  /// </summary>
  /// <param name="service">Drive API service instance.</param>
  /// <param name="title">Title of the file to insert, including the extension.</param>
  /// <param name="description">Description of the file to insert.</param>
  /// <param name="parentId">Parent folder's ID.</param>
  /// <param name="mimeType">MIME type of the file to insert.</param>
  /// <param name="filename">Filename of the file to insert.</param>
  /// <returns>Inserted file metadata, null is returned if an API error occurred.</returns>
  private static File insertFile(DriveService service, String title, String description, String parentId, String mimeType, String filename) {
    // File's metadata.
    File body = new File();
    body.Title = title;
    body.Description = description;
    body.MimeType = mimeType;

    // Set the parent folder.
    if (!String.IsNullOrEmpty(parentId)) {
      body.Parents = new List<ParentReference>()
          {new ParentReference() {Id = parentId}};
    }

    // File's content.
    byte[] byteArray = System.IO.File.ReadAllBytes(filename);
    MemoryStream stream = new MemoryStream(byteArray);

    try {
      FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);
      request.Upload();

      File file = request.ResponseBody;

      // Uncomment the following line to print the File ID.
      // Console.WriteLine("File ID: " + file.Id);

      return file;
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
      return null;
    }
  }