json-csv icon indicating copy to clipboard operation
json-csv copied to clipboard

Adds Content-length Header For Downloads

Open moebrowne opened this issue 3 months ago • 1 comments

When downloading files it is useful to send the content length header so that the browser can give the user some indication of progress, especially with large files

moebrowne avatar Oct 04 '25 11:10 moebrowne

Great idea, but I think it is not correctly implemented as filename is a string. Something like this would be better, but I have not tested it.

public function convertAndDownload(?string $filename = null, bool $exit = true): void
{
    $filename = $filename ?? $this->filename;
    $convertedData = $this->convert();
    $this->sendHeaders($filename, $convertedData);
    echo $convertedData;
    if ($exit === true) {
        exit();
    }
}

protected function sendHeaders(string $filename, string $data): void
{
    header('Content-disposition: attachment; filename=' . $filename . '.' . $this->conversion['extension']);
    header('Content-type: ' . $this->conversion['type']);
    header('Content-length: ' . strlen($data));  // Use strlen() on the actual data
}

ozdemirburak avatar Oct 09 '25 16:10 ozdemirburak