开发者

Django downloaded file is empty

开发者 https://www.devze.com 2023-02-01 12:10 出处:网络
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, bas

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.


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

fileWrap = FileWrapper(xmlFile.read())
0

精彩评论

暂无评论...
验证码 换一张
取 消