0

Here are the lines to read file & download in django :

def download(request):
    file_name =request.GET.get('file_name', '')
    path_to_file = "C:\Users\CV_Uploads\uploadfiles\uploadfiles" + "\\" + file_name

    fileanme = open('r'. path_to_file , "rb")  # **ERROR HERE**
    mimetype = mimetypes.guess_type('path_to_file')[0]
    if not mimetype: mimetype = "application/octet-stream"

    response = HttpResponse(fileanme.read(), mimetype=mimetype)
    response["Content-Disposition"]= "attachment; filename=%s" % os.path.split(path_to_file)[1]
    return response

Problem: Was working fine with hardcoded values but now as i make this code dynamic by joining path_to_file with file_name . It says: AttributeError at /download/ . . . .'str' object has no attribute 'path_to_file'

How to solve this attribute error?

Tameen Malik
  • 1,258
  • 6
  • 17
  • 45

1 Answers1

1

Well, that code is clearly wrong. As the error says, you're taking the string "r", and using the dot notation to call a path_to_file attribute on it, which doesn't exist.

I'm not sure what that r is supposed to be doing at all. It should work fine without it:

fileanme = open(path_to_file , "rb")
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895