Skip to content Skip to sidebar Skip to footer

How To Serve Result File Using Django?

I have developed an App that takes input file from the upload method without using model and run some code behind the server like this: /MyDjango_project_directory/media/input.csv

Solution 1:

After struggling a lot finally I have write down what I needed:

Views would be:

def Download_files(request, file_name):

        file_path = os.path.join('/out_put_files', file_name)
        file_wrapper = FileWrapper(file(file_path,'rb'))
        file_mimetype = mimetypes.guess_type(file_path)
        response = HttpResponse(file_wrapper, content_type=file_mimetype )
        response['X-Sendfile'] = file_path
        response['Content-Length'] = os.stat(file_path).st_size
        response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name) 

        return response

and the URLs:

 url(r'^protocol/(?P<file_name>.+)$', views.Download_files, name='Download_files'),

I am answering my own question this means I just learned this little while ago, and posted here so that if some one can be benefited from this. If any expert came across to this answer kindly review it, wither its a ugly solution or an appropriate solution and will it work during deployment also ?. thanks.

This Question has helped me a lot to understand and implement the concept: Downloading the files(which are uploaded) from media folder in django 1.4.3


Post a Comment for "How To Serve Result File Using Django?"