How do I download a file?

147 views
Skip to first unread message

Sanket Dasgupta

unread,
Jun 7, 2019, 9:36:04 AM6/7/19
to google-apps-sc...@googlegroups.com
I am currently developing a gmail addon. I have a button in the contextual trigger pane that calls an action, In this action's callback function:

Can I download a file (whose whole binary data I have) in the browser? Is there any API that allows you to do so? I am aware of making my own download endpoint in my own server, but right now since I have the whole blob, I want to download it in the browser itself.

I have tried using ContentService.createTextOutput(StringData).downloadAsFile() but this does not work. Even if it does work for text files, it would not work for other binary files would it?

Also, can CacheService can be used for this?

Martin Hawksey

unread,
Jun 7, 2019, 1:56:45 PM6/7/19
to google-apps-sc...@googlegroups.com
Is the file generated in the browser by another site. 

On Fri, 7 Jun 2019, 14:36 Sanket Dasgupta, <ben10...@gmail.com> wrote:
Can I download a file (whose whole binary data I have) in the browser? Is there any API that allows you to do so? I am aware of making my own download endpoint in my own server, but right now since I have the whole blob, I want to download it in the browser itself.

I have tried using ContentService.createTextOutput(StringData).downloadAsFile() but this does not work. Even if it does work for text files, it would not work for other binary files would it?

Also, can CacheService can be used for this?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-script-community.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/b771fe75-81c6-4818-9cb7-b34ee794afca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sanket Dasgupta

unread,
Jun 7, 2019, 3:18:40 PM6/7/19
to google-apps-sc...@googlegroups.com
Hi Martin, yes, if you are asking about the binary data of the file, it is retrieved from a different site.

You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/U9XJGjmMN-o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.

Martin Hawksey

unread,
Jun 8, 2019, 3:23:01 AM6/8/19
to google-apps-sc...@googlegroups.com
Is there a url to directly access the file? If so you can get the file with Apps Script using UrlFetch https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app

If there isn't a URL you can use then this is probably beyond the capabilities of Apps Script

Sanket Dasgupta

unread,
Jun 8, 2019, 3:26:14 AM6/8/19
to google-apps-sc...@googlegroups.com
Right, that makes sense. But would doing this download the actual file? It would just store it in the response object returned by the method call. How do I make the browser download this?

Martin Hawksey

unread,
Jun 8, 2019, 3:43:05 AM6/8/19
to Google Apps Script Community
The highlighted code here might be able to do what you are looking for https://gist.github.com/denilsonsa/8134679#file-url_to_drive-js-L38-L98

In you Apps Script project you'd include the code and then call the following (replacing "YOUR_URL_HERE", "DRIVE_FOLDER_ID", "FILE_NAME" with your values)

uploadToDrive("YOUR_URL_HERE", "DRIVE_FOLDER_ID", "FILE_NAME");

Reply all
Reply to author
Forward
0 new messages