It took me a while how to do this. Figured it by reading several sites
returned by Google. Actually it's quite simple and easy. Here is my
sample code (using django server):

# assume you have an Attachment class and each row in the class
# has a FileField called 'stored_file'.
def attachment(request, the_id):
        try:
                the_file = Attachment.objects.get(id=the_id)
        except:
                raise Http404()
        data = FileWrapper(the_file.stored_file)
        response = HttpResponse(data, mimetype='application/%s' %
the_file.content_type)
        response['Content-Disposition'] = 'attachment; filename=%s' %
the_file.orig_file_name
        return response

On Nov 12, 7:29 pm, vikash rai <vikasloves...@gmail.com> wrote:
> Hi,
>
> I am new to django .
> From html page I want to download a file from a location in my local
> system . I am using django server and not the apache/lightpd.
> By the following code I am able to get the dialouge box on clicking the
> link on web page .
> But it downloads exactly 0 bytes.
> Please suggest what to do ?
>
> *HTML CODE*
>
> <td nowrap>
>          <a href="/mts/cpd/filename">Download Core</a>
>       </td>
>
> *URLS*:
>
> (r'^mts/cpd/filename$','core.srdown')
>
> *views/core.srdown :
>
> *def srdown(request, id):
>
>    paths= str(request.path)
>
>    response = HttpResponse(mimetype='application/force-download')
>    response['Content-Disposition'] = 'attachment; filename=%s' % paths
>
>    response['X-Sendfile'] = paths
>
>    return response*
>
> *I get the box asking for downloading but on clicking it downloads file with
> 0 bytes.
> I have given the absolute path to the file '/mts/cpd/filename'. Is that
> correct ?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to