download.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:eoj3 作者: ultmaster 项目源码 文件源码
def respond_as_attachment(request, file_path, original_filename, document_root=None):
    if document_root is not None:
        file_path = os.path.join(document_root, file_path)
    try:
        fp = open(file_path, 'rb')
        response = HttpResponse(fp.read())
        fp.close()
        type, encoding = mimetypes.guess_type(original_filename)
        if type is None:
            type = 'application/octet-stream'
        response['Content-Type'] = type
        response['Content-Length'] = str(os.stat(file_path).st_size)
        if encoding is not None:
            response['Content-Encoding'] = encoding
            url_encode()
        response['Content-Disposition'] = "attachment; filename*=UTF-8''%s" % iri_to_uri(original_filename)
        return response
    except Exception as e:
        raise Http404(e)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号