首页 > 解决方案 > 这是什么数据?

问题描述

我正在使用 Django,我正在获取一个存储在BinaryField中的 pdf 文件,并尝试将其作为数据类型发送到响应中(最好,我想按照客户的要求以尽可能多的数据类型发送它)。

class CVPdf(generics.UpdateAPIView):
    permission_classes = [IsAuthenticated]
    parser_classes = [FileUploadParser]

    def get(self, *args):
        """
        * ``:param request:`` GET request sent with ``Authorization: Bearer token_id``.
        * ``:return:`` Authenticated Seeker CV file
        """
        pdf_file= CV.objects.get(id=1).pdf_file
        return HttpResponse(pdf_file)

使用 python请求库执行 GET 请求,我得到以下数据:

'...0000000123 65535 f\r\n0000000124 65535 f\r\n0000000125 65535 f\r\n0000000126 65535 f\r\n0000000127 
65535 f\r\n0000000128 65535 f\r\n0000000129 65535 f\r\n0000000130 65535 f\r\n0000000131 65535 
f\r\n0000000132 65535 f\r\n0000000133 65535 f\r\n0000000134 65535 f\r\n0000000135 65535 
f\r\n0000000136 65535 f\r\n0000000137 65535 f\r\n0000000138 65535 f\r\n0000000139 65535 
f\r\n0000000140 65535 f\r\n0000000141 65535 f\r\n0000000142 65535 f\r\n0000000143 65535 
f\r\n0000000144 65535 f\r\n0000000145 65535 f\r\n0000000146 65535 f\r\n0000000147 65535 
f\r\n0000000148 65535 f\r\n0000000149 65535 f\r\n0000000000 65535 f\r\n0000032441 00000 
n\r\n0000032871 00000 n\r\n0000235049 00000 n\r\n0000235493 00000 n\r\n0000236007 00000 
n\r\n0000236479 00000 n\r\n0000432257 00000 n\r\n0000432285 00000 n\r\n0000432726 00000 
n\r\n0000647350 00000 n\r\n0000647920 00000 n\r\n0000648462 00000 n\r\n0000648763 00000 
n\r\n0000661917 00000 n\r\n0000661961 00000 n\r\n0000662439 00000 n\r\n0000778958 00000 
n\r\n0000778986 00000 n\r\n0000798973 00000 n\r\ntrailer\r\n<</Size 169/Root 1 0 R/Info 36 0 
R/ID[<B3EBF57233FC8C4C9A30C5ED4E046BAA><B3EBF57233FC8C4C9A30C5ED4E046BAA>] 
>>\r\nstartxref\r\n799587\r\n%%EOF\r\nxref\r\n0 0\r\ntrailer\r\n<</Size 169/Root 1 0 R/Info 36 0 
R/ID[<B3EBF57233FC8C4C9A30C5ED4E046BAA><B3EBF57233FC8C4C9A30C5ED4E046BAA>] /Prev 799587/XRefStm 
798973>>\r\nstartxref\r\n803127\r\n%%EOF\r\n--ff856adc8f49533d56f779b14dcf63e8--\r\n'

这是什么数据类型?

标签: pythondjangopdftypesbinary

解决方案


在我的一个项目中,我为课堂分数创建了一个文件服务功能:

因此,为文件提供服务的代码部分是:

 with open(file_path, 'rb') as fh:
     response = HttpResponse(fh.read(), content_type="application/default")
     response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
     return response

您要做的就是将 PDFfilepath 放入变量 'file_path' 中,它会起作用(我想)


推荐阅读