如何在下载我上传的数据时自动设置 Content-Type

在处理文件下载并设置 Content-Type 时,可以根据你所使用的后端技术(例如,Java Spring、Node.js、Django 等)进行设置。一般情况下,你可以根据文件类型或扩展名自动设置合适的 Content-Type。

下面我将介绍如何在一些常见的后端框架中实现这个功能。

1、问题背景

在 App Engine 中,我遇到了一个问题,即如何在我下载我上传的数据时自动设置 Content-Type。我的代码如下:

python 复制代码
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

#from login import htmlPrefix,get_current_user

class MyModel(db.Model):
   blob = db.BlobProperty()

class BaseRequestHandler(webapp.RequestHandler):
  def render_template(self, filename, template_args=None):
    if not template_args:
      template_args = {}
    path = os.path.join(os.path.dirname(__file__), 'templates', filename)
    self.response.out.write(template.render(path, template_args))

class upload(BaseRequestHandler):
  def get(self):
    self.render_template('index.html',)
  def post(self):
    file=self.request.get('file')
    obj = MyModel()
    obj.blob = db.Blob(file.encode('utf8'))
    obj.put()
    self.response.out.write('upload ok')
class download(BaseRequestHandler):
    def get(self):
        #id=self.request.get('id')
        o = MyModel.all().get()
        #self.response.out.write(''.join('%s: %s <br/>' % (a, getattr(o, a)) for a in dir(o)))
        self.response.out.write(o)


application = webapp.WSGIApplication(
    [
        ('/?', upload),
        ('/download',download),
    ],
    debug=True
)
def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

My index.html is :
<form action="/" method="post">
<input type="file" name="file" />
<input type="submit" />
</form>

And  it show :
<__main__.MyModel object at 0x02506830>

But I don't want to see this, I want to download it.
How to change my code to run?
Thanks
updated
It is ok now :
class upload(BaseRequestHandler):
  def get(self):
    self.render_template('index.html',)
  def post(self):
    file=self.request.get('file')
    obj = MyModel()
    obj.blob = db.Blob(file)
    obj.put()
    self.response.out.write('upload ok')
class download(BaseRequestHandler):
    def get(self):
        #id=self.request.get('id')
        o = MyModel.all().order('-').get()
        #self.response.out.write(''.join('%s: %s <br/>' % (a, getattr(o, a)) for a in dir(o)))
        self.response.headers['Content-Type'] = "image/png"
        self.response.out.write(o.blob)

And new question is :
if you upload a 'png' file, it will show successful, but when I upload a rar file I will run error. So how to set Content-Type automatically and what is the  Content-Type of the 'rar' file?
Thanks

当我从 html 页面上传一个文件时,我可以成功上传,但是在下载时,我只能看到 __main__.MyModel object at 0x02506830。我想知道如何才能自动设置 Content-Type,以便我能在下载时正确打开文件。

2、解决方案

要自动设置 Content-Type,有两种方法:

  1. 从 self.request.POST 而不是 self.request.get 获取文件
python 复制代码
class upload(BaseRequestHandler):
  def post(self):
    file = self.request.POST['file']
    # ...

这样,你就可以通过 file.type 来获取文件的 Content-Type。

  1. 使用 mimetypes.guess_type() 函数来猜测文件的 Content-Type
python 复制代码
import mimetypes

class upload(BaseRequestHandler):
  def post(self):
    file = self.request.get('file')
    # ...
    content_type, encoding = mimetypes.guess_type(file.filename)

这样,你就可以通过 content_type 来获取文件的 Content-Type。

无论使用哪种方法,你都需要在下载时将 Content-Type 设置到响应头中。

python 复制代码
class download(BaseRequestHandler):
  def get(self):
    # ...
    self.response.headers['Content-Type'] = content_type
    # ...

这样,当用户下载文件时,浏览器就能正确地打开它了。

对于第二个问题,rar 文件的 Content-Type 是 application/x-rar-compressed。你可以在 IANA 网站 上找到更多关于 Content-Type 的信息。

以上示例展示了如何在 Java Spring Boot、Node.js (Express) 和 Python (Django) 中设置文件下载时的 Content-Type。主要思路是根据文件路径或扩展名自动检测 MIME 类型,然后在响应头中添加 Content-Type 和 Content-Disposition 信息。

相关推荐
金銀銅鐵6 分钟前
带您看懂字节码指令(三):一行加法里有什么
java·jvm
文丑颜不良啊11 分钟前
LeetCode2921~2940题目解析
java·leetcode
北漂人Java1 小时前
基于 Spring AI Alibaba WorkFlow 构建 Java 生产级 AI Agent
java·人工智能·spring
Cooper251 小时前
DDC 命令成功但屏幕不动:用 gamma 表重做亮度映射
python·mac
师夷长技以自愈2 小时前
手搓 AI Agent 第 7 篇:菜做砸了别硬炒——错误分类 + 双账本熔断
人工智能·python
用户112936112192 小时前
编译全绿,上线就崩:MyBatis 五层方法名大小写埋下的坑
java
用户956256254292 小时前
15 分钟社媒简报 Agent:我只用了搜索和模型两步
python·api
Dumbness2 小时前
关键词回复里写了 {订单号},群机器人整条消息都炸了
python·微信
调接口的阿光2 小时前
调用对话接口时输出被截断怎么办:先看 finish_reason,再决定是续写一次、缩小范围,还是换成更短的格式(Python)
人工智能·python
Cooper252 小时前
单文件 Flask 跑通微信服务器校验:先跑通再谈工程化
python·flask