Download a file with https

Hi,
I’m using $response->withDownload to have user download file I generated (.csv) but Chrome and other browser refuse to download it because “This file should be served over HTTPS” (since my site is using https).
How do I do that? I’m using cakePHP 4.5
thanks
Michele

Thats a problem with your setup, not cakephp.

E.g. DDEV can be used to have a local setup with a valid HTTPS certificate.

Whats your code?
Seems to be you are creating an absolute (invalid) URL that is http instead of https.
Never do that and instead serve relative URL/path, this way it will stay https and the browser will be fine with it.

Mark, thanks

This is my code (I’m omitting adding all the data to the content string)

public function exportsnewfacultycontactsmeetings()

{
$header = “CWID”;
$filecontent = $header;
$filecontent .=“\n”;


PREPARE ALL THE DATA HERE AS A STRING $filecontent

		$filename = "Faculty_New_Cont_Meet_";
		$filename .= '.csv';

		$response = $this->response;

		 // Inject string content into response body
		$response = $response->withStringBody($filecontent);
		$response = $response->withType('csv');

		 $response = $response->withDownload($filename);

		  // Return response object to prevent controller from trying to render
			 // a view.
		return $response;

}

Suggestions for changes?

The content and response call looks fine actually.
Is it possible that the content is invalid for a CSV and maybe that triggers it?

Interesting idea, but I’m not sure how to test for that.
I can download the file if I use Safari but not Chrome.
Once I download it, it opens fine in Excel without complaints.

Thats super weird.
I never had that issue before

You can also check this live:

Try the csv download. It all works with default settings. The code is also there, maybe you can compare what you did differently.

Hi, I found out what the problem was. In the home page the link to the download procedure was HTTP and not https. It’s an old code, and I had assumed that the httpd would re-write the HTTP part to https like every other link following the httpd.conf but it was not doing it for this(why its another problem)

THANKS to everyone who chimed in, and I apologize for wasting your time with something unrelated to CakePHP (which I love)

Thanks
Michele

1 Like