dostoyevski Posted February 16, 2023 Share Posted February 16, 2023 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 More sharing options...
ps8modules Posted February 19, 2023 Share Posted February 19, 2023 Hello. It won't work for you that way. Create your own tpl e-mail template in the module. Then use the function Mail::send(... Link to comment Share on other sites More sharing options...
dostoyevski Posted February 19, 2023 Author Share Posted February 19, 2023 but, the emails are sent perfectly. What I want is to download the file "ejemploCsv.csv" Link to comment Share on other sites More sharing options...
ps8modules Posted February 19, 2023 Share Posted February 19, 2023 Because header(... it won't work in module. Turn on debug mode and see. Link to comment Share on other sites More sharing options...
dostoyevski Posted February 19, 2023 Author Share Posted February 19, 2023 ok, so, is there a way for downloading the file from a ps module? Link to comment Share on other sites More sharing options...
ps8modules Posted February 20, 2023 Share Posted February 20, 2023 (edited) 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 February 20, 2023 by ps8moduly.cz (see edit history) Link to comment Share on other sites More sharing options...
dostoyevski Posted February 20, 2023 Author Share Posted February 20, 2023 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 Link to comment Share on other sites More sharing options...
Mediacom87 Posted February 21, 2023 Share Posted February 21, 2023 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. Link to comment Share on other sites More sharing options...
ps8modules Posted February 21, 2023 Share Posted February 21, 2023 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 More sharing options...
Mediacom87 Posted February 21, 2023 Share Posted February 21, 2023 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 More sharing options...
ps8modules Posted February 21, 2023 Share Posted February 21, 2023 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. Link to comment Share on other sites More sharing options...
Mediacom87 Posted February 21, 2023 Share Posted February 21, 2023 à 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 More sharing options...
ps8modules Posted February 21, 2023 Share Posted February 21, 2023 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 More sharing options...
ps8modules Posted February 21, 2023 Share Posted February 21, 2023 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 More sharing options...
Mediacom87 Posted February 21, 2023 Share Posted February 21, 2023 il y a une heure, ps8moduly.cz a dit : The code that was modified is already written here in the thread. So, you do well not to share this one since you have not corrected the problems that this module can cause on the stores of those who will install it. Link to comment Share on other sites More sharing options...
ps8modules Posted February 21, 2023 Share Posted February 21, 2023 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. Link to comment Share on other sites More sharing options...
Mediacom87 Posted February 21, 2023 Share Posted February 21, 2023 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 More sharing options...
ps8modules Posted February 21, 2023 Share Posted February 21, 2023 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 More sharing options...
Mediacom87 Posted February 21, 2023 Share Posted February 21, 2023 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 More sharing options...
dostoyevski Posted February 21, 2023 Author Share Posted February 21, 2023 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 More sharing options...
ps8modules Posted February 21, 2023 Share Posted February 21, 2023 And did you install the demo or just overwrite the files? Because it was necessary to register a new hook in the install section. If the hook was not registered, javascript does not work. Link to comment Share on other sites More sharing options...
dostoyevski Posted February 21, 2023 Author Share Posted February 21, 2023 no, i didn't install the hook. That could be the reason. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now