【问题标题】:How do you check the mimetype of a file uploaded to your server?如何检查上传到服务器的文件的 mimetype?
【发布时间】:2010-11-13 02:32:03
【问题描述】:

这只是一个用户上传文件。

【问题讨论】:

    标签: python django linux file unix


    【解决方案1】:

    UploadedFile.content_type 将返回在上传时与文件一起发送的内容类型标头。

    如果您还需要在保存文件后检查文件,您可以使用 python 中的 mimetypes 模块。但它似乎只根据文件扩展名进行检查。

    import mimetypes
    file_type, file_encoding = mimetypes.guess_type('/path/to/file')
    print 'File-type: %s\nFile-encoding: %s' % (file_type, file_encoding)
    

    如果您有默认未检测到的文件类型要求,您也可以在使用 guess_type 之前将类型添加到 mimetypes:

    mimetypes.add_type('font/ttf', '.ttf')
    

    【讨论】:

      【解决方案2】:

      UploadedFile.content_type

      查看http://docs.djangoproject.com/en/dev/topics/http/file-uploads/?from=olddocs了解更多信息

      【讨论】:

        猜你喜欢
        • 2011-04-14
        • 2017-08-17
        • 2014-02-19
        • 1970-01-01
        • 1970-01-01
        • 2019-03-13
        • 1970-01-01
        相关资源
        最近更新 更多