简体   繁体   English

如何通过 HTTP 请求提供临时文件(非静态)?

[英]How to serve a temporary file (non-static) via HTTP request?

I have a Python REST server, that is able to download and write a temporary file using Python TempFile.我有一个 Python REST 服务器,它能够使用 Python TempFile 下载和写入临时文件。 That is, at request time I have aa file in the filesystem of the server, but it is not permanent so the client cannot access it statically (Ie via http://myserver/path-to-my-file ).也就是说,在请求时,我在服务器的文件系统中有一个文件,但它不是永久性的,因此客户端无法静态访问它(即通过 http://myserver/path-to-my-file )。 I need a way to access an endpoint , and then get a file returned based on the request.我需要一种访问端点的方法,然后根据请求获取返回的文件。 (Ie http://myserver/myendpoint/myrequestparameters) (即http://myserver/myendpoint/myrequestparameters)

How does that work over HTTP?这在 HTTP 上如何工作? Is it possible?可能吗?

(For context, right now I am serving the file encoded as string using base64 encoding and utf-8 decoding, but my frontend application needs a direct download link) (对于上下文,现在我使用 base64 编码和 utf-8 解码将文件编码为字符串,但我的前端应用程序需要直接下载链接)

I believe there's a dedicated response type for such stuff in django .我相信在 django 中有专门的响应类型 Assuming send_file is your endpoint:假设 send_file 是您的端点:

from django.http import FileResponse

def send_file(response):

    img = open('my_image_file.jpg', 'rb')

    response = FileResponse(img)

    return response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM