Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

How do I force a download with App Bridge?

Solved

How do I force a download with App Bridge?

PhobosTech
Shopify Partner
80 3 14

First - I apologize, I have another post with a similar question, but I don't think I really spelled it out. And, being that you can't exactly delete old posts, I'm just going to clarify & hopefully I can catch an answer or two.

 

At first glance, this seems like an easy fix - just churn out an anchor tag with the download attribute linking to a previously written file. However, that carries with it some serious security issues.

 

The best option is to build the file out, stick it in memory or some other temp buffer, and force the download by manipulating headers. That's what one would *usually* do.

The problem is: when you're running through the App Bridge, Shopify is intercepting and modifying all sorts of header data and variables. I have tried every possible conceivable way. I've even hacked up a few fairly creative, mildly genius (if I don't say so myself) methods I was almost positive could skirt around the problem - alas, to no avail.

 

This is actually a pretty big problem for me and my client. Would anybody happen to know the secret to forcing downloads when you're running from the App Bridge?

 

Please somebody know ... please please please 🙂

 

Thanks in advance to anybody who could shed some light on this ... "feature" 😉

Most people, it turns out, just aren't interested unless they have to pay for it. Go figure.
Accepted Solution (1)

Henry_Tao
Shopify Staff
91 28 15

This is an accepted solution.

Hi @PhobosTech ,

 

Regarding to downloading a file, I would be interested in learning all of your exploration in details. There is a way that I think it would work is that you build the download url and use App Bridge Remote Redirect with newContext so that it will open your download link as new tab https://help.shopify.com/en/api/embedded-apps/app-bridge/actions/navigation/redirect#redirect-to-an-.... Regarding security concern, you can add an one time use key to the query param so that the link can be only used once. 

 

May I know what file format you want to download? 

 

Thanks, 

Henry

Henry | Social Care @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

View solution in original post

Replies 6 (6)

Henry_Tao
Shopify Staff
91 28 15

This is an accepted solution.

Hi @PhobosTech ,

 

Regarding to downloading a file, I would be interested in learning all of your exploration in details. There is a way that I think it would work is that you build the download url and use App Bridge Remote Redirect with newContext so that it will open your download link as new tab https://help.shopify.com/en/api/embedded-apps/app-bridge/actions/navigation/redirect#redirect-to-an-.... Regarding security concern, you can add an one time use key to the query param so that the link can be only used once. 

 

May I know what file format you want to download? 

 

Thanks, 

Henry

Henry | Social Care @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

PhobosTech
Shopify Partner
80 3 14

Ah, ok. Thank you for the info.

Yes, the file type is csv. I'm building it out on the fly according to the merchant's request.

Most people, it turns out, just aren't interested unless they have to pay for it. Go figure.
Henry_Tao
Shopify Staff
91 28 15

Let us know if the remote redirect approach works for you on all desktop, android, and iOS.

Henry | Social Care @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

CharlyTalavera
Shopify Partner
31 1 9

I tried this and it works before. But it looks like this solution is not longer valid. It's not downloading the files inside Shopify app anymore.

Do you have any solution or workaround for it?

Freddy_dev
Shopify Partner
7 0 6

Remember to skip before action for the download method. As suggested by @Henry_Tao , add an one time use custom key to the query param so that the link can be only used once. 

PhobosTech
Shopify Partner
80 3 14

So, I've obviously tried the usual:

$fullCSV = "inc/temp_csv/generatedCSVs/{$csvFile}.csv";

$readableStream = fopen($fullCSV, 'rb');
$writableStream = fopen('php://output', 'wb');

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"".basename($fullCSV)."\"");
header('Expires: 0');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
header('Content-Length: '.filesize($fullCSV));
flush();
readfile($fullCSV);

stream_copy_to_stream($readableStream, $writableStream);

 Then when I realized what was actually happening here, I tried redirecting with header('Location: csvdownload.php') and forcing a download after a redirect to avoid any output or header changes were happening sometime after the initial buffer output; but, that didn't work. I'm guessing the way Shopify's App Bridge works is a bit different from how I previously had imagined.

Most people, it turns out, just aren't interested unless they have to pay for it. Go figure.