Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 905465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:13:24+00:00 2026-05-15T16:13:24+00:00

I’m trying to make a script for downloading the uploaded files, on the user’s

  • 0

I’m trying to make a script for downloading the uploaded files, on the user’s machine. The problem is that the download simply doesn’t work (it either downloads me an empty file, or gives me some errors).

the last error is:
coercing to Unicode: need string or buffer, FieldFile found

def download_course(request, id):
    course = Courses.objects.get(pk = id).course

    path_to_file = 'root/cFolder'
    filename = course # Select your file here.                                
    wrapper = FileWrapper(file(course))
    content_type = mimetypes.guess_type(filename)[0]
    response = HttpResponse(wrapper, content_type = content_type)
    response['Content-Length'] = os.path.getsize(filename)
    response['Content-Disposition'] = 'attachment; filename=%s/' % smart_str(course)

    return response

how can I declare properly the filename so that it will know each time what file to be downloading:
the filename is actually ‘course’ as declared above

Thanks !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:13:24+00:00Added an answer on May 15, 2026 at 4:13 pm

    edited

    I think that you need to extract path value from FileField object:

    def download_course(request, id):
        course = Courses.objects.get(pk = id).course
    
        path = course.path # Get file path
        wrapper = FileWrapper( open( path, "r" ) )
        content_type = mimetypes.guess_type( path )[0]
    
        response = HttpResponse(wrapper, content_type = content_type)
        response['Content-Length'] = os.path.getsize( path ) # not FileField instance
        response['Content-Disposition'] = 'attachment; filename=%s/' % \ 
                                           smart_str( os.path.basename( path ) ) # same here
    
        return response
    

    Why is that:

    Let’s say I have (well, I actually have) Model:

    class DanePracodawcy( DaneAdresowe, DaneKontaktowe ):
        # other fields
        logo = ImageWithThumbnailsField( upload_to = 'upload/logos/',
                                      thumbnail = {'size': (180, 90)},
                                      blank = True )
    

    ImageWithThumbnailsField is subclass of FileField, so it behaves the same way. Now, when I do SELECT:

    mysql> select logo from accounts_danepracodawcy;
    +-----------------------------+
    | logo                        |
    +-----------------------------+
    | upload/logos/Lighthouse.jpg |
    +-----------------------------+
    1 row in set (0.00 sec)
    

    it shows (relative to MEDIA_ROOT) path of stored file. But when I access logo Model attribute:

    [D:projekty/pracus]|1> from accounts.models import DanePracodawcy
    [D:projekty/pracus]|4> DanePracodawcy.objects.get().logo
                       <4> <ImageWithThumbnailsFieldFile: upload/logos/Lighthouse.jpg>
    [D:projekty/pracus]|5> type( _ )
                       <5> <class 'sorl.thumbnail.fields.ImageWithThumbnailsFieldFile'>
    

    I get instance of some object. If I try to pass that instance to os.path.getsize:

    [D:projekty/pracus]|8> import os.path
    [D:projekty/pracus]|9> os.path.getsize( DanePracodawcy.objects.get().logo )
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    
    D:\projekty\pracus\<ipython console> in <module>()
    
    C:\Python26\lib\genericpath.pyc in getsize(filename)
         47 def getsize(filename):
         48     """Return the size of a file, reported by os.stat()."""
    ---> 49     return os.stat(filename).st_size
         50
         51
    
    TypeError: coercing to Unicode: need string or buffer, ImageWithThumbnailsFieldFile found
    

    I get TypeError, like you. So I need file path as string, which can be obtained with path attribute:

    [D:projekty/pracus]|13> os.path.getsize(  DanePracodawcy.objects.get().logo.path )
                       <13> 561276L
    

    Alternatively, I could get name attribute and os.path.join it with MEDIA_ROOT setting:

    [D:projekty/pracus]|11> from django.conf import settings
    [D:projekty/pracus]|12> os.path.getsize(  os.path.join( settings.MEDIA_ROOT, DanePracodawcy.objects.get().logo.name ) )
                       <12> 561276L
    

    But that’s unnecessary typing.

    Last thing to note: because path is absolute path, I need to extract filename to pass it to Content-Disposition header:

    [D:projekty/pracus]|16> DanePracodawcy.objects.get().logo.path
                       <16> u'd:\\projekty\\pracus\\site_media\\upload\\logos\\lighthouse.jpg'
    [D:projekty/pracus]|17> os.path.basename( DanePracodawcy.objects.get().logo.path )
                       <17> u'lighthouse.jpg'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.