download any type of file from gdrive using C# code
First you have to import ,
using Google.GData.Client;
using Google.GData.Documents;
into your solution. Then use the below code for download the document.
DocumentsService service = new DocumentsService("testAttempt"); service.setUserCredentials("testDrive@gmail.com Email", "password");// Use the proper credential of your //gdrive
// Initialize a query object
Google.GData.Documents.DocumentsListQuery query = new Google.GData.Documents.DocumentsListQuery();
query.Title = test.txt;// search the title begin with d
query.TitleExact = true;
DocumentsFeed feed = service.Query(query);// Query send to Google server
foreach (DocumentEntry entry in feed.Entries)
{
if (entry.Title.Text == "test.txt") // you may check the file name
{
// create the download uri
Uri documentUri = new Uri(entry.Content.Src.Content);
string DocsPath = documentUri.AbsoluteUri;
string resourId = entry.ResourceId.ToString();// if you need the unique resource Id..
HttpWebRequest webRequest = (HttpWebRequest)
HttpWebRequest.Create(documentUri);
GDataGAuthRequestFactory factory = (GDataGAuthRequestFactory)
service.RequestFactory;
webRequest.Headers.Add("Authorization", "GoogleLoginauth=" + factory.GAuthToken);
HttpWebResponse webResponse = (HttpWebResponse)
webRequest.GetResponse();
Stream stream = null;
int bytesToRead = 1024;
byte[] buffer = new Byte[bytesToRead];
byte[] downloadedData = new byte[0];
HttpWebResponse fileResp = (HttpWebResponse)webRequest.GetResponse();
if (webRequest.ContentLength > 0)
fileResp.ContentLength = webRequest.ContentLength;
stream = fileResp.GetResponseStream();
MemoryStream mstream = new MemoryStream();
int length;
while (true)
{
length = stream.Read(buffer, 0, bytesToRead);
if (length == 0)
{
break;
}
else
mstream.Write(buffer, 0, length);
}
downloadedData = mstream.ToArray();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + test.txt + "\"");
HttpContext.Current.Response.ContentType = "text/plain";// specify the type
HttpContext.Current.Response.BinaryWrite(downloadedData);
HttpContext.Current.ApplicationInstance.CompleteRequest();
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
stream.Close();
mstream.Close();
}
}
thanks.
using Google.GData.Client;
using Google.GData.Documents;
into your solution. Then use the below code for download the document.
DocumentsService service = new DocumentsService("testAttempt"); service.setUserCredentials("testDrive@gmail.com Email", "password");// Use the proper credential of your //gdrive
// Initialize a query object
Google.GData.Documents.DocumentsListQuery query = new Google.GData.Documents.DocumentsListQuery();
query.Title = test.txt;// search the title begin with d
query.TitleExact = true;
DocumentsFeed feed = service.Query(query);// Query send to Google server
foreach (DocumentEntry entry in feed.Entries)
{
if (entry.Title.Text == "test.txt") // you may check the file name
{
// create the download uri
Uri documentUri = new Uri(entry.Content.Src.Content);
string DocsPath = documentUri.AbsoluteUri;
string resourId = entry.ResourceId.ToString();// if you need the unique resource Id..
HttpWebRequest webRequest = (HttpWebRequest)
HttpWebRequest.Create(documentUri);
GDataGAuthRequestFactory factory = (GDataGAuthRequestFactory)
service.RequestFactory;
webRequest.Headers.Add("Authorization", "GoogleLoginauth=" + factory.GAuthToken);
HttpWebResponse webResponse = (HttpWebResponse)
webRequest.GetResponse();
Stream stream = null;
int bytesToRead = 1024;
byte[] buffer = new Byte[bytesToRead];
byte[] downloadedData = new byte[0];
HttpWebResponse fileResp = (HttpWebResponse)webRequest.GetResponse();
if (webRequest.ContentLength > 0)
fileResp.ContentLength = webRequest.ContentLength;
stream = fileResp.GetResponseStream();
MemoryStream mstream = new MemoryStream();
int length;
while (true)
{
length = stream.Read(buffer, 0, bytesToRead);
if (length == 0)
{
break;
}
else
mstream.Write(buffer, 0, length);
}
downloadedData = mstream.ToArray();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + test.txt + "\"");
HttpContext.Current.Response.ContentType = "text/plain";// specify the type
HttpContext.Current.Response.BinaryWrite(downloadedData);
HttpContext.Current.ApplicationInstance.CompleteRequest();
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
stream.Close();
mstream.Close();
}
}
thanks.
Post a Comment
1 Comments
Dont work for https://docs.google.com/document/export?format=docx&id={0} or https://docs.google.com/feeds/download/documents/Export?id={0}&exportFormat=docx&format=docx... can you help me? it retuns a login page.
ReplyDelete