0

I'm trying to do what I think is a very simple thing in Django: dynamically create an XML file based on a user request, then allow the user to download that file. I've done tons of Googling and, based on everything I'm seeing, I'm doing it right:

xmlFile = open({PATH_TO_FILE}, 'r')
fileWrap = FileWrapper(xmlFile)
response = HttpResponse(fileWrap, mimetype='text/xml')
response['Content-Disposition'] = 'attachment; filename=data.xml'
response['Content-Length'] = os.path.getsize({PATH_TO_FILE})
return response

The file downloads, but the download is always empty, even though I've confirmed that the file exists on disk in the correct location and is not empty. I've tried this in 3 different browsers (Firefox, Safari, Chrome), just in case it's a browser thing, but the result is the same each time. Argh.

  • 1
    @Eric O'Connor: why are you storing the file on disk to load it after that? Wouldn't it be better to simply redirect the user to the `xml` file if you've already stored it as a file? And if that wasn't your purpose, than you should simply return the xml response as you create it. – Wolph Dec 30 '10 at 00:45
  • I've just noticed this topic aswell, so this seems to be a duplicate: http://stackoverflow.com/questions/3213060/django-download-file-not-working/3213548#3213548 – Wolph Dec 30 '10 at 00:46
  • I did notice that other issue, but it's not a duplicate. In that issue, the person wasn't passing their file information to the FileWrapper class correctly. Specifically, they were trying to allow the user to download a file represented by a FileField object, and they weren't accessing the field data correctly. And in answer to your question, I don't want the file to be served statically by the web server; I want the download to be processed by Django, because there's some additional processing I need to do. – Eric O'Connor Dec 30 '10 at 00:50
  • Did you assure as well that the file has the correct content after being stored? – Bernhard Vallant Dec 30 '10 at 00:58
  • Yes, the contents of the file on disk are correct. – Eric O'Connor Dec 30 '10 at 17:11
  • First of all, WoLpH said it better than I could-- you should probably just return the XML file as a response while you create it. – C. Alan Zoppa Dec 30 '10 at 17:41

1 Answers1

0

See my comment to your original post, but it may work if you change line 2 to this:

fileWrap = FileWrapper(xmlFile.read())
C. Alan Zoppa
  • 823
  • 8
  • 11