1

I am trying to download the xlsx blob file from azure storage through my browser using FastAPI in the backend:

def download_blob(blob_service_client, filename: str, container: str):
    try:        
        blob_client = blob_service_client.get_blob_client(container=container, blob=filename)
        blob_url = blob_client.url
        return FileResponse(content=blob_url, status_code=200, media_type='application/vnd.ms-excel', filename="Test.xlsx")
    except Exception as e:
        return JSONResponse(content={"message": "File is not downloaded."}, status_code=500)

# blob_url looks like this
blob_url = "https://testapp.blob.core.windows.net/test-container/test-sample.xlsx"

but I am getting status code 500 in response.

anybody guide me how can I download the file?

Chris
  • 18,724
  • 6
  • 46
  • 80
Himanshu
  • 71
  • 9
  • 1
    You could use [`RedirectResponse`](https://fastapi.tiangolo.com/es/advanced/custom-response/#redirectresponse). – Chris Sep 30 '22 at 17:48
  • @Chris - I used the RedirectResponse(blob_url) and got this response:-- `Failed to fetch. Possible Reasons: CORS Network Failure URL scheme must be "http" or "https" for CORS request. I have used: from fastapi.middleware.cors import CORSMiddleware app = FastAPI() origins = ["*"] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], )` – Himanshu Sep 30 '22 at 18:43
  • @Chris - Not working either – Himanshu Oct 02 '22 at 12:23
  • @Chris - I am using Swagger UI. and I have mentioned the code in my question. Do let me know if you need anything else – Himanshu Oct 02 '22 at 12:49
  • Please have a look at [this answer](https://stackoverflow.com/a/73608593/17865804), in case you are using Swagger UI to test your API, as well as [this answer](https://stackoverflow.com/a/70777217/17865804). – Chris Oct 02 '22 at 13:14
  • Does this answer your question? [How to redirect the user to another page after login using JavaScript Fetch API?](https://stackoverflow.com/questions/75184430/how-to-redirect-the-user-to-another-page-after-login-using-javascript-fetch-api) – Chris Mar 05 '23 at 13:48

0 Answers0