Closed absaravanan closed 1 year ago
I figured it out. My download API was not returning Content-Length
I wanted to express my gratitude for your recent contributions to the community, particularly regarding your assistance with resolving the Content-Length issue in your download API. Your efforts have been truly valuable.
I'm currently facing a similar issue in which I'm attempting to access an API directly from a URL by modifying the path name and extracting content using the following code
However, when I alter the pathname, it fails to return any content, even though I believe the path is accurate:
If it's necessary to host and then access a specific file, like "truck.splat," could you kindly provide details on how you achieved this in your case? Your insights would be greatly appreciated.
Thank you once again for your assistance.
Here is a simple file hosting service that I used. I hosted it behind nginx with CORS enabled.
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.responses import FileResponse
import os
import shutil
from fastapi.responses import StreamingResponse
app = FastAPI()
# Directory to store uploaded files
UPLOAD_DIR = "uploads"
@app.post("/upload/")
async def upload_file(file: UploadFile):
try:
# Ensure the upload directory exists
os.makedirs(UPLOAD_DIR, exist_ok=True)
# Save the uploaded file to the upload directory
with open(os.path.join(UPLOAD_DIR, file.filename), "wb") as f:
shutil.copyfileobj(file.file, f)
return {"message": "File uploaded successfully", "filename": file.filename}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/download/{filename}")
async def download_file(filename: str):
try:
# Check if the file exists in the upload directory
file_path = os.path.join(UPLOAD_DIR, filename)
if not os.path.exists(file_path):
raise HTTPException(status_code=404, detail="File not found")
# Get the size of the file
file_size = os.path.getsize(file_path)
print(file_size)
# Return the file as a StreamingResponse with the Content-Length header set
return StreamingResponse(
open(file_path, "rb"),
headers={
"Content-Disposition": f"attachment; filename={filename}",
"Content-Length": str(file_size),
},
media_type="application/octet-stream", # Adjust media type as needed
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=9000)
Thank you for your response. Since I'm at a beginner level, I require some additional assistance. I've successfully set up my FastAPI server, and it's currently running at http://127.0.0.1:8000/. However, when I attempt to run the command uvicorn main:app --host 0.0.0.0 --port 9000 --reload, I encounter a "page not found" error. At present, my server is operational using the FastAPI code you provided. What steps should I take next?
I've also uploaded my "truck.splat" file, and I'd like to access it from the frontend using a URL like http://127.0.0.1:5500/truck, where the "splat view" should display the 3D truck model. Below are the relevant code snippets:
Frontend JS code:
Backend FastAPI code:
My "truck.splat" file is located in the upload folder and appears like this:
I would greatly appreciate your guidance on this matter.
@absaravanan It would be appreciated if you spare some amount of your precious time for the issue i am facing.
I was able to view my .ply file in the viewer and also download the model.splat file. But when I try to open the model.splat as a hosted URL it throws range error.