send_file and send_data

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from django.http import HttpResponse

import os, mimetypes

def send_data(path, filename = None, mimetype = None):

    if filename is None: filename = os.path.basename(path)
    
    if mimetype is None:
        mimetype, encoding = mimetypes.guess_type(filename)
    
    response = HttpResponse(mimetype=mimetype)
    response.write(file(path, "rb").read())
    return response
    
def send_file(path, filename = None, mimetype = None):

    if filename is None: filename = os.path.basename(path)

    if mimetype is None:
        mimetype, encoding = mimetypes.guess_type(filename)
    
    response = HttpResponse(mimetype=mimetype)
    response['Content-Disposition'] = 'attachment; filename=%s' %filename
    response.write(file(path, "rb").read())
    return response

More like this

  1. A view for downloading attachment by achimnol 3 years, 8 months ago
  2. Send large files through Django, and how to generate Zip files by jcrocholl 5 years, 9 months ago
  3. HttpResponseSendfile by eternicode 1 year ago
  4. Decorator to make files downloadable by prigun 4 years, 6 months ago
  5. Simple "html email with images" sender by andres_torres_marroquin 2 years, 2 months ago

Comments

jcrocholl (on August 18, 2007):

For large files, you may want to use FileWrapper, like in the following snippet that I wrote: http://www.djangosnippets.org/snippets/365/

#

(Forgotten your password?)