Upload files to Google Drive using Google Drive API in ASP.Net and C# .Net

Last Reply on Dec 17, 2014 12:05 AM By Mudassar

Posted on Dec 16, 2014 03:10 AM

Hello

 

i need one solution

 

for example there is one form for CV upload and user will come to my site and upload there CV and all that CV autometcly save on my google drive.

How can i do this in asp.net

 

can you please guide me

 

Thanks

Posted on Dec 17, 2014 12:05 AM Modified on on Dec 19, 2014 09:13 AM

You can do it using the ASPSnippets.GoogleAPI.

1. Download the latest DLL from here

Download File

2. Then use the following code.

HTML

<form id="form1" runat="server">
File:
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<br />
Description:
<asp:TextBox ID="txtDescription" runat="server" Width="300"></asp:TextBox>
<hr />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="UploadPhoto" />
<hr />
<table id="tblFileDetails" runat="server" visible="false" border="0" cellpadding="0"
cellspacing="0">
<tr>
    <td>
        Title
    </td>
    <td>
        <asp:Label 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="false">
    <td valign="top">
        Thumbnail
    </td>
    <td>
        <asp:Image ID="imgThumbnail" runat="server" Height="60" Width="60" />
    </td>
</tr>
<tr>
    <td>
        Created Date
    </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>
</form>

Namespaces

using ASPSnippets.GoogleAPI;
using System.Web.Script.Serialization;

Code

protected void Page_Load(object sender, EventArgs e)
{
    GoogleConnect.ClientId = "<Google Client ID>";
    GoogleConnect.ClientSecret = "<Google Client Secret>";
    GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split('?')[0];
    GoogleConnect.API = EnumAPI.Drive;
    if (!string.IsNullOrEmpty(Request.QueryString["code"]))
    {
        string code = Request.QueryString["code"];
        string json = GoogleConnect.PostFile(code, (HttpPostedFile)Session["File"], Session["Message"].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 = true;
            imgThumbnail.ImageUrl = file.ThumbnailLink;
        }
    }
    if (Request.QueryString["error"] == "access_denied")
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Access denied.')", true);
    }
}

protected void UploadPhoto(object sender, EventArgs e)
{
    Session["File"] = FileUpload1.PostedFile;
    Session["Message"] = txtDescription.Text;
    GoogleConnect.Authorize("https://www.googleapis.com/auth/drive.file");
}

public class GoogleDriveFile
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string OriginalFilename { get; set; }
    public string ThumbnailLink { get; set; }
    public string IconLink { get; set; }
    public string WebContentLink { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime ModifiedDate { get; set; }
}

Screenshots

1. File details displayed with Download link on page

2. File being shown in drive with the Description