-5

I want to upload files from my ASP.NET project using the Google Drive API.

How do I use DriveService in ASP.NET?

I searched lot, but I didn't get proper results for DriveService.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chandran
  • 21
  • 1
  • 6

1 Answers1

1

Try https://developers.google.com/drive/quickstart-cs and http://code.google.com/p/google-api-dotnet-client/

And from 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;
    }
  }
pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • Thanks for your help, my main issue is creating DriveService instance, how to create instance to DriveService please help me – Chandran Jan 15 '14 at 06:21
  • If you click the links I pasted, you would find "var service = new DriveService(new BaseClientService.Initializer()" – pinoyyid Jan 15 '14 at 06:44