web2py/pydal.Download uploaded file error when the original filename of uploaded file is chinese character

104 Views Asked by At

I am using web2py. I cloned the latest version of pydal. I define a 'upload' field in my table. Then I upload a image whose filename is chinese character. Everything seems ok so far. However when I try to access the uploaded file with the download function, I get a server error. I have found two solutions to avoid this error. The first solution is changing the fllename to english character, the second solution is downgradeing the pydal to the older version embedded in web2py(2.14.6,release at May,10,2016). So I guess something is wrong in the latest pydal when handling filename with chinese character. Hope to get your answer. Thank you.

1

There are 1 best solutions below

0
On

Could it be that the problem here actually has to do with HTTP? Because response.download will put your filename in Content-Disposition and although many browsers accept it, only ASCII is actually acceptable here.

If this is the problem the solution here is to pass a download_filename where you percent encode the chinese characters. So in your download function you would do something like this:

def download():
    import urllib
    return response.download(request, db, download_filename=urllib.quote(request.args(0))

I haven't actually tested it but it should work.