如何在下载我上传的数据时自动设置 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-TypeContent-Disposition 信息。

相关推荐
源代码•宸7 分钟前
分布式缓存-GO(分布式算法之一致性哈希、缓存对外服务化)
开发语言·经验分享·分布式·后端·算法·缓存·golang
我送炭你添花16 分钟前
Pelco KBD300A 模拟器:03.Pelco-P 协议 8 字节完整拆解 + 与 Pelco-D 一一对应终极对照表
python·测试工具·运维开发
云和数据.ChenGuang18 分钟前
PHP-FPM返回的File not found.”的本质
开发语言·php·运维工程师·运维技术
It's now18 分钟前
Spring AI 基础开发流程
java·人工智能·后端·spring
cxh_陈19 分钟前
线程的状态,以及和锁有什么关系
java·线程·线程的状态·线程和锁
计算机毕设VX:Fegn089521 分钟前
计算机毕业设计|基于springboot + vue图书商城系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
R.lin34 分钟前
Java 8日期时间API完全指南
java·开发语言·python
毕设源码-赖学姐40 分钟前
【开题答辩全过程】以 高校教学质量监控平台为例,包含答辩的问题和答案
java·eclipse
高山上有一只小老虎1 小时前
翻之矩阵中的行
java·算法
yangpipi-1 小时前
《C++并发编程实战》 第4章 并发操作的同步
开发语言·c++