zoukankan      html  css  js  c++  java
  • 返回pdf文档

    code

    #coding=utf-8
    from django.shortcuts import render
    from django.http import HttpResponse
    from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
    from django.utils import timezone
    from django.shortcuts import render,redirect
    from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
    from django.shortcuts import render_to_response
    from booklist.models import bookinfoA as A
    from booklist.models import bookinfoB as B
    from booklist.models import bookinfoC as C
    import os,sys
    sys.path.append("/".join(os.path.dirname(os.path.abspath(__file__)).split("/")[:-1])+"/lib")
    from encrype import encry
    from django.http import StreamingHttpResponse
    from django.utils.encoding import escape_uri_path
    
    
    
    
    def file_iterator(file_name, chunk_size=512):
            with open(file_name,'rb') as f:
                while True:
                    c = f.read(chunk_size)
                    if c:
                        yield c
                    else:
                        break
    
    
    def extractfile(request,book_type,book_dir,book_id,book_name,book_format):
        enc=encry()
        book_id=int(enc.getdecrype(book_id))
        book_type=chr(enc.getdecrype(book_type))
    
    
        #book_dir=enc.decrypt_data(book_dir).decode()
        #print(book_id)
        #print(book_type)
        #print(book_dir)
        #print(timezone.now().strftime('%Y%m%d'))
    
    
        filepath="/".join(os.path.dirname(os.path.abspath(__file__)).split("/")[:-1])+"/files/"
    
    
        file_dir="/".join(list(book_dir.split('#')[0]))
    
    
        file=book_name+"."+book_format
        the_file_name = filepath+file_dir+"/"+file
    
    
        response = StreamingHttpResponse(file_iterator(the_file_name))
        response['Content-Type'] = 'application/octet-stream'
        response['Content-Disposition'] = 'attachment;filename="{0}"'.format(escape_uri_path(file))
        return response

  • 相关阅读:
    firefox 使用过程
    如何在Ubuntu 18.04中安装VMware Workstation Player
    pycharm 设置
    matplotlib函数理解
    Linux:从入门到放弃
    Pycharm 项目无法导入自己写的模块(问题记录贴)
    代数之管见(孟道骥)第二讲:漫谈数学学习
    numpy学习总结(重点讲解索引方式)
    ubuntu中vi下删除键和上下左右键的异常解决
    Docker使用问题记录贴
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14182274.html
Copyright © 2011-2022 走看看