Jump to content

download a file from a prestashop module


Recommended Posts

I am trying to download a file from a prestashop module. For that I have this code. It finds the file but doesn't download it.

			$directorioActual = _PS_UPLOAD_DIR_;
            $filename = $directorioActual."ejemploCsv.csv";
            if (!file_exists($filename)) {
                mail("[email protected]", "el file no existe", "failed");
            } else {
                mail("[email protected]", "el file si existe", "success");
            }
            
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($filename).'"');
            header('Content-Length: ' . filesize($filename));
            readfile($filename);

 

Link to comment
Share on other sites

Why do you think that if you give a piece of code without specifying that we will give you a quality solution?
Is it the configuration of the module or is it the controller of the module or the class of the module?
How is CSV generated, as plain text or fputcsv ?
Should the CSV be available from a URL, or just generate and download the file?
Is it used in the JavaScript module?
This is only some of the information that is needed.

 

in module:

$csv = $this->getCSVData(); /* generated CSV as text with separator */
$def = array('csvData' => $csv, 'csvName' => 'my-csv-file.csv');
Media::addJsDef($def);

in javascript:

$(document).ready(function(){
  	$( "#my-button-download-generated-csv" ).click(function(e) {
      	downloadCSVFileGenerated(csvData, csvName);
    });
});

function downloadCSVFileGenerated(csvData, csvName){
    let a = document.createElement('a');
    a.setAttribute('style', 'display:none;');
    document.body.appendChild(a);
    let blob = new Blob([csvData], { type: 'text/csv' });
    let url = window.URL.createObjectURL(blob);
    a.href = url;
    a.download = csvName;
    a.click();    
}

 

Edited by ps8moduly.cz (see edit history)
Link to comment
Share on other sites

13 hours ago, dostoyevski said:

Sorry for not specifying well. Let's see if I clarify it now: The csv is generated as plain text. The csv should be downloaded by clicking the "Save settings" button. I don't use javascript at all. And finally here is the complete code of the module on github: source code

I sent you the modified module in a private message.

Link to comment
Share on other sites

Il y a 2 heures, ps8moduly.cz a dit :

I sent you the modified module in a private message.

Why in private message, why not share it here to benefit the community?

It could help new developers to compare the two versions and even, you could propose it on the initial GitHub to make it evolve.

Link to comment
Share on other sites

à l’instant, ps8moduly.cz a dit :

Because I am not the author of the module and only the author can share the code. Only received the edit and sample data for csv download. I respect ownership.

 

However, by sharing this one privately, you are indeed violating what you say.

In any case, the license used by this code allows you to modify it and distribute it while maintaining just the information of the initial source.

So nothing prevents you from sharing it if you respect the initial license.

Link to comment
Share on other sites

13 hours ago, Mediacom87 said:

Hi,

I'm not sure what the code of the module you sent can be used for, frankly, I wouldn't use it because it has so many errors or approximations in its development.

A great expert in the creation of Prestashop modules and can not advise.
Why don't you write what is wrong with the module and how it should be?
You'll help the community and budding programmers.
Maybe even experienced module programmers do something wrong and are happy to learn if you write them your advice.

Are you just a critic and like to spam threads?

Link to comment
Share on other sites

4 hours ago, Mediacom87 said:

However, by sharing this one privately, you are indeed violating what you say.

In any case, the license used by this code allows you to modify it and distribute it while maintaining just the information of the initial source.

So nothing prevents you from sharing it if you respect the initial license.

And do you know who is the author of the sample module on github?

The code that was modified is already written here in the thread.

Link to comment
Share on other sites

il y a 11 minutes, ps8moduly.cz a dit :

I still don't understand why you rate, criticize, but you don't write anything yourself.
The whole community is waiting for you to tell me what's wrong.

Take the right step and your professionalism.

It is true that I have done absolutely nothing for this community in 15 years, you are quite funny in the end.

Link to comment
Share on other sites

I don't see any help from you in this thread or in other posts.
We don't care how long you've been in the community, but 15 years doesn't sit well with me. You have been here since a year
2013 and it's 2023, that's ten years (9.5 years).
So tell us your experience.
So far you're just writing nonsense.
By the time I'm 50 years old, I've experienced many people who make themselves world champions.
Now you have enriched my collection with another one.

Link to comment
Share on other sites

il y a 12 minutes, ps8moduly.cz a dit :

I don't see any help from you in this thread or in other posts.
We don't care how long you've been in the community, but 15 years doesn't sit well with me. You have been here since a year
2013 and it's 2023, that's ten years (9.5 years).
So tell us your experience.
So far you're just writing nonsense.
By the time I'm 50 years old, I've experienced many people who make themselves world champions.
Now you have enriched my collection with another one.

You don't know anything about my history or PrestaShop's history but you don't hesitate to attack me personally.

I did not attack you, I just gave my opinion about some elements of this topic which I think are justified to alert the readers of the forum.

You are right, it has not been 15 years, but more than 15 years since my registration on this forum dates back to January 8, 2008 by being the 167ᵉ member of this forum, but in more than 15 years, a lot happens in a community.

Link to comment
Share on other sites

sorry ps8moduly.cz but your code didn't worked for me. In the end I resorted to chatgpt and this is the solution that works:

			$filename = $directorioActual . "ejemploCsv.csv";

            $download_filename = 'myfile.csv';
            $download_path = _PS_DOWNLOAD_DIR_ . $download_filename;
            $file_content = file_get_contents($filename);
            file_put_contents($download_path, $file_content);

            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename=' . basename($download_path));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($download_path));
            readfile($download_path);

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...