返回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

 

 

 

 

 

 

 

 

 

posted @ 2020-12-24 00:29  anobscureretreat  阅读(124)  评论(0编辑  收藏  举报