13
I am using django to design the basic web pages that handles the uploading and downloading of the files to/from the media folder
Actually the files are uploaded successfully in to the media folder, also files are downloaded successfully but an underscore is appended to the file_name as a last charater like file_one.pdf_ , file_two.pdf_ , file_three.txt_ etc.,
Below are my codes
urls.py
urlpatterns = patterns('',
url(r'^upload$', 'learn_django.views.upload'),
url(r'^files_list$', 'learn_django.views.files_list'),
url(r'^download/(?P<file_name>.+)$', 'learn_django.views.download'),
)
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
) + urlpatterns
views.py
def upload(request):
......
....
return render_to_response('uploads_form.html', {'form': form},context_instance=RequestContext(request))
def files_list(request):
return render_to_response('files_list.html',{'total_files':os.listdir(settings.MEDIA_ROOT),'path':settings.MEDIA_ROOT},context_instance=RequestContext(request))
def download(request,file_name):
file_path = settings.MEDIA_ROOT +'/'+ 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
files_list.html
<table border="1" colspan="2" width="100%">
<tr>