Forums

[Errno 36] File name too long (Base64)

I've been trying to generate a word document via HTML forms, using python-docx and BeautifulSoup 4. The only problem is when the user copy some Base64 images (URL images work fine). When testing locally, the program works fine, on PythonAnywhere, the error "File name too long" appears.

I'm a novice at python and it's my first time uploading in python anywhere.
Here's the code:

def convert_image(type, img, doc):
    if type == 1: 
        # URL
        img_bytes = requests.get(img).content
    else: 
        # Base64
        img_bytes = base64.b64decode(img)

    image = io.BytesIO(img_bytes)

    document = doc
    pic = document.add_picture(image)
    if pic.width > Inches(5):
        DownloadView.scale_picture(pic, Inches(5))

And yeah, the Base64 string is HUGE.
What can I do to solve this error or process the data in a better way?

Which line is generating the error?

Sorry I took so long to answer.
I'm having trouble debugging here in python anywhere, but my tests reported the error as soon as the method is called.
The log I'm seeing isn't very detailed... (the string is incomplete as well)

2021-08-13 13:59:11 [Errno 36] File name too long: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAggAAAOQCAYAAABcvon5AAAgAElEQVR4Aey9CfTlVXXn+2dUFBxJ08nL685rn6ZXNPG916azOknHF1+001kxESQJiDKKRkWNQzSAmCgFOAHiCMRZZIig4pSoEQoQaYUqJpkUpcCSoSiqikI06ffWOm/tW/Utdu3fOb/f786/372fWuvWOWfvffY595zP/Z3vHf73rvz04Z+nedwOPWbVYFwrdYvzkF2x0a+29/u6+XM5vM3He7ty50rfJ/rlUy75Y1v2XKlY5VKM2iW/xUWf+vgcipFN/VT6PrlY9ZPPxyuHSsWoT7T7vopV6fuMUn/OAa/ZiW1r+5vPGWO9j/p8rhGsO+sOA/NlYGVeG5A7BPxhEefV1ufjfH2YfBbb1Nf8uvnc0ZZr+/imuvVXTMxl9jp/Ll65hu3rx1Ffb4t131Z8aWz5fR+r+7bv26YuIaDYYdvqRznfCxTrz/rDwPwYmKtAiBtfdyC09fk4Xx9mLIsdtW/s19SO84pt39/XFedtvp7zy6bSx/u6+cdpx76j5Mv10bzblMMKghjfZgxi5nfhYu1ZexiYPgOdFgh20PhbCQh/IPm6xat/7BvjfGypj3Lk+pZ8MTa21c+XGl+lfLm+3ubrpT7KqbIuTj4rY27196XiY2ypv+Jz/pLN92mq26HvYyQCVEafb1Of/sWHNWaNYaDbDHRWIMRDJrY9WN7n66UYs8e4pnZdrjrfMHmb5hVzxfhh/T7e12PeNm2L0S3matO/TR/lb1uWhEBJOLTNS9wje81asBYwsLgMIBC2H2zxgIpt/yAYxhdjY9vntXr0+7avq5+3Wb2prX5xLN8v+krt2Ee5c/Zoy7W9zeq+rdzDlhIJUR

That then suggests to me that you have some code to catch exceptions and just print the exception without the full traceback. Find where you're doing that and, maybe only temporarily, remove the try..except so that you can get the full traceback.

In general, it looks to me like you're trying to use the base64 as a filename, instead of the content of the file. From the code you posted above, that would suggest that the type that is being passed in is incorrect, so add some logging to the code to see what the values of the variables are and make sure that they are what you expect.

Thank you glenn, I'll try that ASAP and return here with what I find.
By the way, I'm from Brazil. I'm sorry if my english is broken and if I take too long to answer.