2

I am developing an web application with Python and Django web framework. There is a part where I have to export the database table content in a .csv format file to a local disk. I wonder if there is a way to show an file browser window to let the user picking where(which directory) he/she wants to save this file by clicking a "button(eg. choose directory)" on webpage?

I am confused whether I should achieve this feature through Python, Django, or JavaScript.

And I hope it returns a path(string) to the location where the user wants to store this file.

update- I already know how to export the contents into a .csv file. I pass this cmd to the database: "copy " + table_name + " to '" + path + "' delimiter ',' csv header;" and it does the job. So I just need the path where user selects. I could ask the user to manually type the path in a textbox and submit the form, but it's much better if a file browser window can pop up and let the user select where to store the file.

jetyue777
  • 35
  • 7
  • JS would probably be easiest. There are plenty of modal dialogs out there, and I'm sure somebody has written a jQuery file picker, so look around. AFAIK writing it in Python/Django wouldn't work, as there's no way for the user to pick the location. XHR is your best bet. – MattDMo Sep 11 '14 at 21:24

2 Answers2

1

Yes, of course you can do it. Take a look at official documentation:

https://docs.djangoproject.com/en/1.7/howto/outputting-csv/

Ihor Pomaranskyy
  • 5,437
  • 34
  • 37
  • thanks for the reply, I already know how to export the contents into a .csv file. I pass this cmd to the database: "copy " + table_name + " to '" + path + "' delimiter ',' csv header;" and it does the job. So I just need the path where user selects. I could ask the user to manually type the path in a textbox and submit the form, but it's much better if a file browser can pop up and let the user select where to store the file. – jetyue777 Sep 11 '14 at 21:05
  • Do you want to store the file on server, or you want to download it? – Ihor Pomaranskyy Sep 11 '14 at 21:09
  • Then you should return the data to user via HttpResponse in some view. The data could be taken from some file or streamed. Usually files are downloaded to default location, but it depends on browser. There is no way to set the path where file will be downloaded (because of security resons — imagine you have possibility to force user to store content from your site overwriting users' local configuration files, for example hosts..). – Ihor Pomaranskyy Sep 12 '14 at 05:10
  • Have you found a solution on this? I am trying to do exactly the same. – user1919 May 30 '16 at 19:50
  • Up to date link https://docs.djangoproject.com/en/3.2/howto/outputting-csv/ – Deepstop Aug 14 '21 at 23:27
0

I think you should use StreamingHttpResponse or as suggested by @Igor Pormaranskiy a simple HttpResponse.

eg : http://django.readthedocs.org/en/1.7.x/howto/outputting-csv.html

and http://djangotricks.blogspot.com/2013/12/how-to-export-data-as-excel.html

iMitwe
  • 1,218
  • 16
  • 35