Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

python - Downloading the files(which are uploaded) from media folder in django 1.4.3

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>
     <th width="60%">File</td>
     <th width="40%">Download</td> 
   </tr>
 {% for file in total_files %}
   <tr>
     <td width="60%">{{file}}</td>
     <td width="40%" align="center"><a href="/download/{{file}}" style="text-decoration:None">Download here</a></td>
   </tr>
 {% endfor %}  
</table>

So in the above codes, when a file is uploaded successfully in to media , it will be redirected to files_list.html through files_list view functions which displays the total number of files in the form of table with a download link beside to each file name.

So when we click on the download anchor link the appropriate file will be downloaded by executing the function download .

So the file is downloading sucessfully , but an underscore _ is appending to the last of the file name like file_one.pdf_ , file_two.pdf_ , file_three.txt_ etc.,.

So can anyone please let me know, what's wrong in my above download function code and why underscore is appending to the file name and how to remove that underscore from the file name...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your code is right but there is one redundant character in download:

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

At last line the filename attribute has a trailing slash (/): filename=%s/

Which causes the problem. Remove this slash and it works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...