Is there a way to force a download link instead of the browser trying to open the file? In this case I have a docs spreadsheet and a some links to mp3 files. I want the users to
To expand on Mokarom's answer (because I can't comment) you need to replace the part of those links that say "yourDocId" with the document id of the page you're trying to download. This is the key= part of the url.
For example, if you had a spreadsheet at https://docs.google.com/spreadsheet/ccc?key=0AhjOs40Xk1-4dGg0MjMVYURPMFRpeUFhbmZBTkZ0dEE#gid=8, then you would go to https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=0AhjOs40Xk1-4dGg0MjMVYURPMFRpeUFhbmZBTkZ0dEE&exportFormat=csv for the download link.
In my app I needed to download a Google Drive's spread sheet in the form of a CSV file (Comma Separated Values). Google Drive's files are referenced by long ID's such as:
1uFwmwD5UFLdD1DMPGT5p4fIGozfImx44DNCOeMJr1Lo
Here is a code block that will do this. If there is an error the method will return a zero length string.
- (NSString *) downloadGoogleSheetGivenFileID: (NSString *) ID {
// This will download a Google spreadsheet given it's file ID
// and convert it to a CSV file.
NSString * preFix;
NSURL * myURL;
NSString * opCmd;
NSString * t;
NSString * theStuff;
NSError * error = nil;
theStuff = @"";
// The URL has to be: prefix+ID+opCmd The only thing that changes is the file's ID
preFix = @"https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=";
opCmd = @"&exportFormat=csv";
t = preFix;
t = [t stringByAppendingString:ID];
t = [t stringByAppendingString:opCmd];
myURL = [NSURL URLWithString:t];
theStuff = [NSString stringWithContentsOfURL:myURL encoding: NSUTF8StringEncoding error:&error];
if(error != nil)
{
// There was an error downloading the spread sheet file.
// We will return a zero length string.
NSLog(@"%@",error);
}
return theStuff;
}
To make download links for documents - https://docs.google.com/feeds/download/documents/Export?docID=yourDocId
To make download links for any file - https://docs.google.com/uc?id=yourDocId&export=download&hl=en_US
To make download links for spreadsheets - https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=yourDocId&exportFormat=csv
For further information please go here
UPDATING IN 2018
The new download URL is
https://docs.google.com/spreadsheets/d/<KEY>/export?gid=<GID>&format=csv
where <KEY>
and <GID>
can be obtained from your navigation's URL,
https://docs.google.com/spreadsheets/d/<KEY>/edit#gid=<GID>
PS: spreadsheets (workbook) may have multiple sheets (worksheets), GID is the desired sheet ID. One-sheet-spreadsheet usually has gid=0
, but if you add more they'll have random numbers (the GID is preserved even changing tab-order).
So, using wget
or curl
you can test,
wget --no-check-certificate -O test.csv \
'https://docs.google.com/spreadsheets/d/0At2sqNEgxTf3dEt5SXBTemZZM1gzQy1vLVFNRnludHc/export?gid=0&format=csv'