In this article I will explain how to upload files to Google Bulldoze using Google Drive API in ASP.Net with C# and VB.Cyberspace and the ASPSnippets.GoogleAPI.

Getting Google Customer ID and Client Hugger-mugger

In order to use Google Drive API for uploading files to Google Drive, you will demand to create an Application in Google Console and go Client ID and Client Cloak-and-dagger. For details please refer the post-obit article.

Downloading ASPSnippets.GoogleAPI DLL

You can download the ASPSnippets.GoogleAPI API DLL using the following download link.

HTML Markup

The HTML Markup consist of an ASP.Cyberspace FileUpload control, a TextBox and a Push for uploading the File forth with its Description to the Google Drive.

There'south also an HTML Table containing some Label controls for displaying the details of the uploaded file.

File:

< asp : FileUpload ID ="FileUpload1" runat ="server" />

< br />

< br />

Description:

< asp : TextBox ID ="txtDescription" runat ="server" Width ="300"></ asp : TextBox >

< hr />

< asp : Push button ID ="btnUpload" runat ="server" Text ="Upload" OnClick ="UploadFile" />

< hr />

< table id ="tblFileDetails" runat ="server" visible ="false" edge ="0" cellpadding ="0"

cellspacing ="0">

< tr >

< td >

        Championship

</ td >

< td >

< asp : Characterization ID ="lblTitle" runat ="server" />

</ td >

</ tr >

< tr >

< td >

        Id

</ td >

< td >

< asp : Label ID ="lblId" runat ="server" />

</ td >

</ tr >

< tr >

< td >

        Icon

</ td >

< td >

< asp : Image ID ="imgIcon" runat ="server" />

</ td >

</ tr >

< tr id ="rowThumbnail" runat ="server" visible ="simulated">

< td valign ="tiptop">

        Thumbnail

</ td >

< td >

< asp : Image ID ="imgThumbnail" runat ="server" Height ="60" Width ="threescore" />

</ td >

</ tr >

< tr >

< td >

        Created Appointment

</ td >

< td >

< asp : Label ID ="lblCreatedDate" runat ="server" />

</ td >

</ tr >

< tr >

< td >

        Download

</ td >

< td >

< asp : HyperLink ID ="lnkDownload" Text ="Download" runat ="server" />

</ td >

</ tr >

</ table >

Namespaces

You volition need to import the following namespaces.

Note : You will need place the ASPSnippets.GoogleAPI DLL within the BIN folder of your projection and add together its reference.

C#

using ASPSnippets.GoogleAPI;

using Organization.Web.Script.Serialization;

VB.Net

Imports ASPSnippets.GoogleAPI

Imports System.Web.Script.Serialization

Data Class

You will demand to create the following class which will exist used to hold the Google Bulldoze file details returned from Google API after the file is uploaded.

The construction of this class is same as that of the JSON string returned from the Google API and so that the JSON string can be easily deserialized to its object.

C#

public class GoogleDriveFile

{

public string Id { go; set; }

public string Championship { get; set up; }

public cord OriginalFilename { go; ready; }

public cord ThumbnailLink { become; set up; }

public string IconLink { get; ready; }

public string WebContentLink { get; set; }

public DateTime CreatedDate { go; prepare; }

public DateTime ModifiedDate { become; set; }

}

VB.Cyberspace

Public Class GoogleDriveFile

Public Holding Id() Every bit Cord

Get

Return m_Id

Cease Get

Set(value As String)

            m_Id = Value

End Set

Finish Belongings

Individual m_Id As Cord

Public Belongings Title() Every bit String

Get

Return m_Title

Finish Get

Gear up(value Equally String)

            m_Title = Value

End Prepare

End Property

Individual m_Title Equally String

Public Property OriginalFilename() As String

Go

Return m_OriginalFilename

End Get

Set(value Every bit String)

            m_OriginalFilename = Value

Terminate Set

Stop Property

Private m_OriginalFilename As Cord

Public Property ThumbnailLink() As String

Get

Return m_ThumbnailLink

End Go

Prepare(value Equally String)

            m_ThumbnailLink = Value

End Set up

Cease Belongings

Private m_ThumbnailLink Every bit String

Public Property IconLink() As String

Go

Return m_IconLink

Stop Become

Set(value As Cord)

            m_IconLink = Value

End Set up

End Property

Individual m_IconLink Equally String

Public Belongings WebContentLink() As String

Get

Render m_WebContentLink

Terminate Become

Gear up(value Every bit String)

            m_WebContentLink = Value

Terminate Set

End Property

Private m_WebContentLink Every bit String

Public Holding CreatedDate() As DateTime

Get

Return m_CreatedDate

End Get

Set(value As DateTime)

            m_CreatedDate = Value

End Set

Terminate Property

Private m_CreatedDate As DateTime

Public Property ModifiedDate() As DateTime

Get

Return m_ModifiedDate

End Get

Set(value As DateTime)

            m_ModifiedDate = Value

Stop Set

Cease Property

Private m_ModifiedDate As DateTime

End Class

Cosign user using Google business relationship

On the click of the Upload button, User is redirected to the Google Authorization page where user has to provide permission to the Application to access his Google Bulldoze.

For this article I am requesting access to the Google Drive of the user by passing the https://www.googleapis.com/auth/drive.file scope. User can allow and deny and in both cases he is sent back to the URL set as the RedirectUri while creating the application in Google Developer Panel.

The uploaded File and the Description is saved in Session variable for uploading the file to Google Drive afterwards the permission is granted.

C#

protected void UploadFile(object sender, EventArgs due east)

{

    Session["File"] = FileUpload1.PostedFile;

    Session["Clarification"] = txtDescription.Text;

GoogleConnect.Qualify("https://world wide web.googleapis.com/auth/bulldoze.file");

}

VB.Net

Protected Sub UploadFile(sender As Object, e As EventArgs)

    Session("File") = FileUpload1.PostedFile

    Session("Description") = txtDescription.Text

GoogleConnect.Qualify("https://www.googleapis.com/auth/drive.file")

Terminate Sub

Upload Files to Google Drive using Google Drive API in ASP.Net with C# and VB.Net

Uploading the File to Google Drive and displaying the details on page

The very offset matter is that yous demand to set the Client ID and the Client Secret for the GoogleConnect class and yous need to set the API as Drive every bit we need to utilise the Google Drive API.

The below code looks for access lawmaking (access token) in Query string and and so this access code is passed to the PostFile role of the GoogleConnect grade along with the File and its Description from the Session variable.

The PostFile function returns the uploaded Google Bulldoze file details equally JSON string which is and so deserialized to the GoogleDriveFile form object.

Finally the details of the uploaded Google Drive file are displayed.

C#

protected void Page_Load(object sender, EventArgs due east)

{

GoogleConnect.ClientId = "<Google Client ID>";

GoogleConnect.ClientSecret = "<Google Client Clandestine>";

GoogleConnect.RedirectUri = Asking.Url.AbsoluteUri.Split('?')[0];

GoogleConnect.API = EnumAPI.Drive;

if (!cord.IsNullOrEmpty(Request.QueryString["lawmaking"]))

    {

string lawmaking = Asking.QueryString["code"];

string json = GoogleConnect.PostFile(lawmaking, (HttpPostedFile)Session["File"], Session["Description"].ToString());

GoogleDriveFile file = (new JavaScriptSerializer()).Deserialize<GoogleDriveFile>(json);

        tblFileDetails.Visible = true;

        lblTitle.Text = file.Title;

        lblId.Text = file.Id;

        imgIcon.ImageUrl = file.IconLink;

        lblCreatedDate.Text = file.CreatedDate.ToString();

        lnkDownload.NavigateUrl = file.WebContentLink;

if (!string.IsNullOrEmpty(file.ThumbnailLink))

        {

            rowThumbnail.Visible = truthful;

            imgThumbnail.ImageUrl = file.ThumbnailLink;

        }

    }

if (Request.QueryString["fault"] == "access_denied")

    {

        ClientScript.RegisterClientScriptBlock(this.GetType(), "warning", "alert('Access denied.')", true);

    }

}

VB.Internet

Protected Sub Page_Load(sender Every bit Object, eastward Every bit EventArgs) Handles Me.Load

GoogleConnect.ClientId = "<Google Client ID>";

GoogleConnect.ClientSecret = "<Google Client Secret>";

GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split up("?"c)(0)

GoogleConnect.API = EnumAPI.Drive

If Not String.IsNullOrEmpty(Request.QueryString("lawmaking")) Then

Dim code As Cord = Request.QueryString("code")

Dim json As String = GoogleConnect.PostFile(lawmaking, DirectCast(Session("File"), HttpPostedFile), Session("Description").ToString())

Dim file As GoogleDriveFile = (New JavaScriptSerializer()).Deserialize(Of GoogleDriveFile)(json)

        tblFileDetails.Visible = True

        lblTitle.Text = file.Championship

        lblId.Text = file.Id

        imgIcon.ImageUrl = file.IconLink

        lblCreatedDate.Text = file.CreatedDate.ToString()

        lnkDownload.NavigateUrl = file.WebContentLink

If Not String.IsNullOrEmpty(file.ThumbnailLink) Then

            rowThumbnail.Visible = True

            imgThumbnail.ImageUrl = file.ThumbnailLink

End If

Cease If

If Request.QueryString("mistake") = "access_denied" And then

        ClientScript.RegisterClientScriptBlock(Me.[GetType](), "alert", "alarm('Admission denied.')", Truthful)

End If

End Sub

Screenshots

The details of the uploaded file displayed on page

Upload Files to Google Drive using Google Drive API in ASP.Net with C# and VB.Net

The uploaded file with description in Google Drive

Upload Files to Google Drive using Google Drive API in ASP.Net with C# and VB.Net

Demo

Downloads