Custom GridField action to download file

Silverstripe Version: 4.10.8

Question: What should I add to handleAction to create and download a file in a GridField?

I would like to create a GridField custom action that downloads a markdown text file created on-the-fly from a DataObject. I feel like I should be able to do something like this:

public function handleAction(GridField $gridField, $actionName, $arguments, $data)
{
        if ($actionName == 'dofiledownloadaction') {

        // get item
        $item = $gridField->getList()->byID($arguments['RecordID']);

        // update last downloaded timestamp
        $item->LastDownloaded = DBDatetime::now()->getTimestamp();
        $item->write();

        // send file for download
        $fileData = $item->getMarkdown(); // returns string of markdown from item
        $filename = "download.md"
        return HTTPRequest::send_file($fileData, $fileName, 'text/markdown');
    }
}

I’ve exhausted all places I can look to see how to approach something like this :slight_smile:

Ok, I worked it out and all I needed to do was add no-ajax as an extra class on the custom Download action button like this:

public function getColumnContent($gridField, $record, $columnName)
{
    if (!$record->canEdit()) {
        return;
    }

    $field = GridField_FormAction::create(
        $gridField,
         'FileDownloadAction'.$record->ID,
        '',
        "dofiledownloadaction",
        ['RecordID' => $record->ID]
    )->addExtraClass('btn--icon-md font-icon-down-circled btn--no-text grid-field__icon-action action-menu--handled no-ajax');  // add 'no-ajax' class here to disable the ajax response that was messing things up

    return $field->Field();
}