1.4.3

2013-03-06 3 views
5

मैं Django का उपयोग कर रहा बुनियादी वेब पृष्ठों है कि uploading और फ़ाइलों के downloading को/संभालती media फ़ोल्डर1.4.3

असल में से डिजाइन करने के लिए Django में मीडिया फ़ोल्डर से फ़ाइलें (जो अपलोड किए जाते हैं) डाउनलोड कर रहा है फ़ाइलें मीडिया फ़ोल्डर में में, यह भी फ़ाइलों को सफलतापूर्वक डाउनलोड कर रहे हैं सफलतापूर्वक अपलोड कर रहे हैं, लेकिन एक underscore रूप file_name में जोड़ा जाता है एक last charaterfile_one.pdf_, file_two.pdf_, file_three.txt_ आदि,

नीचे मेरी कोड हैं जैसे

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> 
तो ऊपर कोड में

, जब एक फ़ाइल सफलतापूर्वक लोड किया जाता है मीडिया में, इसेपर रीडायरेक्ट कर दिया जाएगाfiles_list के माध्यम से कार्य देखें जो प्रत्येक फ़ाइल नाम के बगल में डाउनलोड लिंक के साथ तालिका के रूप में फ़ाइलों की कुल संख्या प्रदर्शित करता है।

तो जब हम डाउनलोड एंकर लिंक पर क्लिक करते हैं तो उपयुक्त फ़ाइल download फ़ंक्शन निष्पादित करके डाउनलोड की जाएगी।

तो फ़ाइल को सफलतापूर्वक डाउनलोड कर रहा है, लेकिन एक underscore_file_one.pdf_, file_two.pdf_, file_three.txt_ आदि जैसे फ़ाइल नाम के अंतिम करने के लिए जोड़कर किया जाता है।,।

तो किसी को भी बस फ़ाइल नाम के बाद / को दूर मुझे पता है, क्या मेरे ऊपर डाउनलोड समारोह कोड में गलत क्या है और क्यों underscorefile name को जोड़कर किया जाता है और कैसे है कि underscore फ़ाइल नाम से निकालने के लिए कृपया कर सकते हैं ...

उत्तर

6

बदलें इस:

response['Content-Disposition'] = 'attachment; filename=%s/' % smart_str(file_name) 

यह करने के लिए:

response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name) 
3

आपका कोड सही है लेकिन 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 

अंतिम पंक्ति फ़ाइल नाम विशेषता पर एक पिछला स्लैश है (/): filename=%s/

समस्या का कारण बनता है। इस स्लैश को हटा दें और यह काम करता है।

संबंधित मुद्दे