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 4 years, 7 months ago
  2. Decorator to make files downloadable by prigun 5 years, 5 months ago
  3. Admin Download as CSV File by msaron 2 years, 2 months ago
  4. FileInput with download like (like the fileinput on admin) by zodman 3 years, 1 month ago
  5. HttpResponseSendfile by eternicode 1 year, 11 months ago

Comments

(Forgotten your password?)