7

As per the requirement ,the client is provide with an exe-(which contains the url of file to be downloaded) If we use the google api, we need to provide additional references along with the exe. It is not allowed by the client.

I used the webclient to download the file, but its downloading the file with doc type as HTML. My requirement is to download the file form google drive without using google API. Is there any way to do that?

I have tried the code as below

 WebClient wb = new WebClient();          
 wb.DownloadFile("https://drive.google.com/file/d/0BzpAdEg-KyDjNVVSb0FBOWQ4V0k/view?usp=sharing", @"C:\TFS\test\test.pdf");

Result as shown below

enter image description here

Shebin
  • 3,310
  • 2
  • 24
  • 27
  • 1
    more curiosity than anything else, but is there a reason you arent using the api? – Takarii Jan 28 '16 at 08:46
  • @Takarii: As per the requirement the client is provide with an exe-(which contains the url of file to be downloaded) If we use the google api, we need to provide additional references along with the exe. It is not allowed by the client. – Shebin Jan 28 '16 at 10:30
  • i think your problem lies with the fact that the link itself doesnt link to the file directly. The link only points to the location where you can select the file itself. – Takarii Jan 28 '16 at 11:15
  • 1
    To get around having other references, you could try embedding them into the exe - http://stackoverflow.com/questions/189549/embedding-dlls-in-a-compiled-executable - would that meet the requirements? – Balah Jan 28 '16 at 11:46
  • 1
    @Takarii I am also curious. I would love to know the _technical_ reasoning that the client is using to demand one file and one file only. I would also love to know what their plan is should someone accidentally move or unshare the document. Either way, the URL will change. – Gusdor Nov 21 '16 at 15:30
  • It didn't work for me even I converted it to download url. I am trying to download encrypted text file and an exe file by using C# WebClient.DownloadFile(). – Pabitra Dash Apr 10 '18 at 05:21

1 Answers1

15

This should help you.

Below is the standard sharing url for google drive.

https://drive.google.com/file/d/FILE_ID/edit?usp=sharing

the format you need to use for direct downloads is:

https://drive.google.com/uc?export=download&id=FILE_ID

this will link directly to the file instead of to the drive view in your current example.

so, for example, if your sharing url is https://drive.google.com/file/d/ABCDEFG1234567/edit?usp=sharing

then your direct download url is https://drive.google.com/uc?export=download&id=ABCDEFG1234567

Note, this is only for files you have uploaded yourself that were not created in drive itself (ie, doc, presentation, spreadsheet) using the google docs. to do that the format changes (will update answer if you need it)

Edit:

It is worth noting that if the files location changes then the url and direct download link will change too, meaning you will need to update it.

Takarii
  • 1,612
  • 2
  • 18
  • 29
  • It didn't work for me even I converted it to download url. I am trying to download encrypted text file and an exe file by using C# WebClient.DownloadFile(). – Pabitra Dash Apr 10 '18 at 05:20
  • @PabitraDash You must have made a mistake with your URL formatting then – Takarii Apr 11 '18 at 22:15
  • The url is as follows. It always download as HTML but its a encrypted text file. https://drive.google.com/uc?export=download&id=1k0V0sBr2F2XDEeL-0qcdjwG1ZomLcAxb&alt=media – Pabitra Dash Apr 12 '18 at 04:10
  • @PabitraDash but you aren't doing a manual download. Format the URL as I already said. – Takarii Apr 15 '18 at 20:15
  • I formatted URL as per your instruction only but still not working. – Pabitra Dash Apr 16 '18 at 04:59
  • @PabitraDash No, its not. Your URL has `&alt=media` at the end. If you need it to download "as is" then drop that part. On top of that if you haven't shared the file properly, then you also wont get a result. Chances are you are getting an html page because thats where the link actually takes you, not to the file. – Takarii Apr 17 '18 at 10:13