#Operation that downloads csv

1 messages ยท Page 1 of 1 (latest)

cerulean flicker
#

Trying to create a manual Flow that reads data, then downloads to the client's browser. I tried creating a custom endpoint extension and sending the flow data there, but the webhook operation just sends the file stream back to the Flow object as data, rather than initiating a download...

Anyone have any thoughts on how this might be achieved? Thanks!

twin ingot
#

Great question ๐Ÿค” The current "manual" trigger flow handler in the app doesn't "care" about the response or its headers[^1], so I don't think there's a solid way to automatically trigger a download from that. It sounds like a great little enhancement to the app to have it be aware of the content-disposition header (and similarly allow configuring that in the flow), so you can trigger any download from a manual trigger hook ๐Ÿ™‚

[^1]: It just waits for the request to be done, and shows a notification based on 2xx or 4xx/5xx tatus

#

That way you could also start getting creative with file streams or other data transformations in script operations for example ๐Ÿค”

cerulean flicker
#

I started working on a Download operation extension that reuses the ExportService. I think that would be the ideal setup, but got stuck. Basically, the idea was to configure a download operation with a property that has data (from $last, or the flow object, similar to how you set it the Console operation). The config could also have a dropdown for choosing json/xml/csv.
Something like this:

if (downloadFormat === 'json') {
res.attachment(${filename}.json);
res.set('Content-Type', 'application/json');
return res.status(200).send(exportService.transform(flowData, 'json'));
}

        if (downloadFormat === 'xml') {
            res.attachment(`${filename}.xml`);
            res.set('Content-Type', 'text/xml');
            return res.status(200).send(exportService.transform(flowData.data, 'xml'));
        }

        if (downloadFormat === 'csv') {
            res.attachment(`${fileName}.csv`);
            res.set('Content-Type', 'text/csv');
            return res.status(200).send(exportService.transform(flowData, 'csv'));
        }
twin ingot
cerulean flicker
#

Sorry for not understanding. - I thought The flow trigger was where we configured if the flow should be Manually triggered.

I was hoping to make a Download operation node possible in any Flow. For example , you could use a Read Data operation, manipulate the data with e Script operation, and finally use the Download operation. Or have it triggered by another flow etc.

I'm fighting with how to initiate a new fs stream response to the browser within the context of an operation though... I'm pretty new to node/express... Maybe it's just not feasible yet (looking to your first comment)?

#

Maybe an alternative would be to use the existing Webhook operation, send it to a custom endpoint extension that instead of downloading the file to the browser, creates a Directus file, spits back the file id...

twin ingot
#

Sorry for not understanding. - I thought The flow trigger was where we configured if the flow should be Manually triggered.

It is! That's also where the configuration lives for the app to know how to render that button, and what to do with the output of the API call. The app needs to be aware it's supposed to download the data that was returned from the API endpoint somehow. I think the trigger configuration is where that will live, as that's our app touch point ๐Ÿค”

#

I was hoping to make a Download operation node possible in any Flow. For example , you could use a Read Data operation, manipulate the data with e Script operation, and finally use the Download operation. Or have it triggered by another flow etc.

This is very interesting, as the Read operation might not be directly called by the end user, and i'm not sure if we can force the browser to download stuff like that in the middle of a request (definitely not when using the app directly) ๐Ÿค”

#

I'm fighting with how to initiate a new fs stream response to the browser within the context of an operation though... I'm pretty new to node/express... Maybe it's just not feasible yet (looking to your first comment)?
I don't think this is feasible at the moment. The way that would work is that the app needs to be aware that it's supposed to save the output of the API, so instead of waiting for the request to be done and rendering a notification, it can prompt the user to save the output.