1

I am getting the excel file in the request body, but when I try to read through openpyxl or xlrd, it is raising the error.

The FastAPI endpoint:

def upload_file(request: Request, file: UploadFile = File(...), db: Session = Depends(get_db)):
    
    for part in decoder.MultipartDecoder(request.get("body").encode('utf-8'), request.get("headers").get("Content-Type")).parts:
        content = part.text
        file_open = pd.read_excel(content.encode('utf-8'), engine='xlrd')
        pc_sheet_names = file_open.sheet_names 

The error:

 File "/var/lang/lib/python3.9/zipfile.py", line 1361, in _RealGetContents
    raise BadZipFile("Bad magic number for central directory")
Chris
  • 18,724
  • 6
  • 46
  • 80
  • How big is your file ? – brushtakopo Nov 09 '22 at 07:36
  • What if you saved the file locally before? – brushtakopo Nov 09 '22 at 09:14
  • i try to save the file and read back again its giving error Not valid file [Errno 22] Invalid – saraswathi moram Nov 09 '22 at 09:52
  • I am getting the data and after encoding the body of request its like : b'----------------------------758679477641211471359369\r\nContent-Disposition: form-data; name="file"; filename="ssn-121212121.xlsx"\r\nContent-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n\r\nPK\x03\x04\x14\x00\x06\x00\x08\x00\x00\x – saraswathi moram Nov 09 '22 at 09:53
  • Where do you save the file, in /tmp folder? – brushtakopo Nov 09 '22 at 10:20
  • i have tried two ways, i) directly reading from the body of request by encoding 2)and i stored to /tmp/some_file.xlsx and read both given the same issue. As my understanding we need to do some way to remove the content of file ----------------------------758679477641211471359369\r\nContent-Disposition: form-data; name="file"; filename="ssn-121212121.xlsx"\r\nContent-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n\r\n , but not getting how to do – saraswathi moram Nov 09 '22 at 12:33
  • @saraswathimoram Please have a look at [this answer](https://stackoverflow.com/questions/63048825/how-to-upload-file-using-fastapi/70657621#70657621) and [this answer](https://stackoverflow.com/a/70667530/17865804) on how to upload files in FastAPI. You don't have to read the raw request body, and even if you did, you could always use `request.form()` to parse the body, which would get you a `FormData` object that you can use to get the `File` and/or `Form` data (see Option 1 of [this answer](https://stackoverflow.com/a/74015930/17865804) for more details). – Chris Nov 09 '22 at 16:36
  • Hi Chris, I was tried this way , but in aws lambda UploadFile is not working, its giving the lambdaContext object, and same way request coming as dictionary and it does'nt have the form – saraswathi moram Nov 09 '22 at 18:15
  • Have a look [here](https://stackoverflow.com/q/61660586/17865804). – Chris Nov 10 '22 at 19:01
  • I have added the APIGway configuration as well as mentioned in the above ticket, and still facing the same issue, file is coming into request.get("body") with boundary and form-data – saraswathi moram Nov 10 '22 at 20:20
  • https://stackoverflow.com/q/74348340/17865804 this is not duplicate @chris, both are different scenario's, any one way if able read the file, also fine for me. – saraswathi moram Nov 11 '22 at 16:10

0 Answers0