Empty file when sending it with Flask on the Vercel platform

I have this simple fuction

def return_srt_file(data, filename) -> Response:
    buffer = BytesIO()
    buffer.write(data.encode("utf-8"))
    buffer.seek(0)

    resp = make_response(buffer.read())
    resp.headers["Content-Disposition"] = f"attachment; filename={filename}.srt"
    resp.headers["Content-Type"] = "application/x-subrip"
    resp.headers["Content-Length"] = str(len(data.encode("utf-8")))

    return resp

that on my local pc, returns a subtitle file. Whole subtitle is set to a string in the data variable. It has only 39kbs. But when I try to download it on my deployed app, I’m getting an empty file - 0b. In logs I can see content of the file.


I think I tried all possible methods of sending a file, including saving it in the /tmp directory. Nothing works, it is always returned as a 0b file

Can anyone help me?

Hi @skoruppa, welcome to the Vercel Community!

If you are able to read the file and log contents on Vercel. It means that the file is available to the serverless function, so no issues there.

Without looking at the full code example, I think buffer.getvalue() should be able to get the full content. Can you try the following code changes once?

from flask import make_response
from io import BytesIO
from flask import Response

def return_srt_file(data, filename) -> Response:
    if not data:
        return make_response("No data to return", 400)

    buffer = BytesIO()
    buffer.write(data.encode("utf-8"))
    buffer.seek(0)

    resp = make_response(buffer.getvalue())
    resp.headers["Content-Disposition"] = f"attachment; filename={filename}.srt"
    resp.headers["Content-Type"] = "application/x-subrip"
    resp.headers["Content-Length"] = str(len(data.encode("utf-8")))

    return resp

If you need more help, please share your public repo or a minimal reproducible example. That will let us all work together from the same code to figure out what’s going wrong.

1 Like

Hello @anshumanb :slight_smile:

Thanks for taking some of your time to try to help me. I applied your code and deployed it. And now an example endpoint, that should return data (as it does locally) returns

No data to return

although I clearly see data in Vercel’s runtime logs ><

As for the code, no problem, my repository is public: GitHub - skoruppa/napisy24-stremio-addon

Just out of curiosity, I tried the same codebase with Leapcell, and it worked there fine, without any issues

Hi @skoruppa, sorry for the delay. I haven’t been able to try out your code yet. Were you able to solve this issue?

No. And I think I tried everything. What is interesting that if I provide smaller object for my function, it works just fine. But still, 32kb, it’s not mutch