1

Currently I'm working on Google Drive integration with Salesforce. I was able to connect to Google Drive and retrieve a list of file information. now I need to download some files from Google Drive. For that I'm using following method:

public PageReference downloadFile(FileWrapper file){
    //webContentLink
    if(file == null || file.id == null){
        return null;
    }

    Http http = new Http();
    HttpRequest req = new HttpRequest();
    req.setMethod('GET');
    req.setEndpoint('https://www.googleapis.com/drive/v2/files/'+ file.id+'?alt=media');
    req.setHeader('Content-Type', 'application/json');
    req.setHeader('Authorization', 'Bearer ' + accessToken);
    req.setTimeout(60 * 1000);

    HttpResponse resp = null;
    resp = http.send(req);
    test = resp.getBody();

    if(resp.getStatusCode() == 200){
        PageReference pageref = new PageReference(resp.getBody());
        return pageref;
    }

    return null;
}

When I run this method I'm getting Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 189.844KB. Is there are any other way to download file other than using PageReference? Please help me to sort out this problem. If you guys have any other better way please suggest.

  • 1
    did you look at transient modifier? – cropredy Feb 5 '16 at 2:37
  • If possible remove test = resp.getBody(); as this variable is increasing the viewstate size and is not used anywhere. – Saroj Bera Sep 8 '16 at 5:50
1

You're giving the PageReference constructor the file content, rather than the URL it wants, but the key here is to not get the file content in Apex at all.

You should be able to have the browser do the work for you by returning a PageReference for the file's webViewLink URL (I think it's webViewLink, but if that doesn't work you might want to try webContentLink) and passing the access token in a URL parameter. Your code would be something like this:

public PageReference downloadFile(FileWrapper file){
    if(file == null || file.webViewLink == null){
        return null;
    }

    String url = file.webViewLink + '?access_token=' + accessToken;

    PageReference pageref = new PageReference(url);
    return pageref;
}
  • Hi metadaddy, thanks for you reply. I tried this way it works when when we long in to google drive. But if we didnt log in to google drive it gives access denied error. I'm sending access token as url parameter as you said – Wishwa Sigera Feb 6 '16 at 7:34
  • If I get a change I'll try it myself. I should be able to generate a file listing and see if I can get it working. – metadaddy Feb 8 '16 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.