Skip to content

Can't manage to download attachment file  #804

Open
@Sulray

Description

@Sulray

Description

I've seen another issue recently with a similar subject but the context is different so we'll see.

I want to download an excel report file from my frontend but I don't know why it's not working as I call the method generated in my client.

Here is a screenshot showing that it's working when i'm directly calling the endpoint from my autogenerated swagger:
image

And here are the response headers I receive when I click on my button (with method "onClick") in my frontend:
image

Even if the headers are the same (with content-disposition: attachment...), it's not downloading the file from my browser when using the frontend.

Reproducible example or configuration

Method to generate the excel file in the fastapi backend:

@router.get("/excel", response_class=StreamingResponse)
async def download_excel_report(background_tasks: BackgroundTasks, db=Depends(get_db)):
    """
    Get xlsx (excel) file of the current state of objects in database
    """
    df = await generate_panda_report(db=db)  # return a panda dataframe
    buffer = BytesIO()
    with pd.ExcelWriter(buffer) as writer:
        df.to_excel(excel_writer=writer, index=False)
    background_tasks.add_task(cleanup, buffer)
    return StreamingResponse(
        BytesIO(buffer.getvalue()),
        media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        headers={
            "Content-Disposition": f"attachment; filename=report.xlsx"
        },
    )

def cleanup(buffer: BytesIO) -> None:
    buffer.close()

Npm command to generate the client from autogenerated openapi doc:

"generate-client": "NODE_TLS_REJECT_UNAUTHORIZED=0 openapi-ts --input https://backend_ip/openapi.json --output ./src/client --client axios"

In the generated client:

export class ReportService {    
    /**
     * Download Excel Report
     * Get xlsx (excel) file of the current state of objects in database
     * @returns unknown Successful Response
     * @throws ApiError
     */
    public static downloadExcelReportReportExcelGet(): CancelablePromise<$OpenApiTs['/report/excel']['get']['res'][200]> {
        return __request(OpenAPI, {
            method: 'GET',
            url: '/report/excel'
        });
    }
    
}

In React, method defined as "onClick" on a button:

    const handleDownloadExcelReport = async(): Promise<void> => {
        try {
            const updatePromise = ReportService.downloadExcelReportReportExcelGet()
            await updatePromise
        } catch(error:unknown) {
            console.error(`Failed to download file: ${error}`);
        }
    }

OpenAPI specification (optional)

No response

System information (optional)

  • OS: Ubuntu 22.04.4 LTS
  • Node: v20.11.1
  • NPM: 10.2.4
  • Hey API: "@hey-api/openapi-ts": "^0.42.1"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🔥Something isn't workinghelp wanted 🙋This issue is unlikely to be prioritized, please contribute a pull request if you'd like it resolved

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions