Force download link on a google docs spreadsheet

前端 未结 3 627
旧巷少年郎
旧巷少年郎 2021-02-08 06:24

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

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-08 06:46

    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;
    

    }

提交回复
热议问题