file download using django and javascript
Xristos Xristoou
saxri89 at gmail.com
Thu May 17 17:40:11 EDT 2018
i have create a django app with REST JSON API and i fill html table in web page from my JSON.
here some examples
javascript snippets :
var field22=document.getElementById('f22');
field22.innerHTML=e.target.feature.properties.id;
var field23=document.getElementById('f23');
field23.innerHTML=e.target.feature.properties.file_1;
html :
</tbody></table><br> <br>
<table style="width:95%">
<tbody><tr>
<th align="left">id :</th>
<td id="f22"></td>
</tr> <tr>
<th align="left">file :</th>
<td id="f23"></td>
</tr> <tr>
i have parse my JSON in table with success but in the file field now i have a simple text from image path.
Now i want in this path to have some hyperlink(or button) for download this file.
any idea how to do this because i stack ?i dont know how to connection javasscript with my download or how to download this file using javascript
django app code
models.py:
class MyModel(models.Model):
file_1 = models.FileField(upload_to='documents/',blank=True, null=True)
simple test django donwload :
def download(request, id):
product_file=MyModel.objects.get(pk=id)
f = StringIO()
zip = zipfile.ZipFile(f, 'w')
product_file_url = product_file.file_1.url
file_url = settings.MEDIA_ROOT + product_file_url[6:]
filename = product_file_url[6:].split('/')[-1]
zip.write(file_url,filename)
zip.close()
response = HttpResponse(f.getvalue(), content_type="application/zip")
response['Content-Disposition'] = 'attachment; filename=file-download.zip'
return response
More information about the Python-list
mailing list