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

  • 相关阅读:
    PowerShell里的数组
    题解 cf gym 103261 D FFT Algorithm
    欧拉筛求积性函数的一般形式
    题解 LOJ 6053
    题解 SP34112 UDIVSUM
    题解 luogu P5162 WD与积木
    Java 关闭流同步输入输出
    题解 luogu P6620 [省选联考 2020 A 卷] 组合数问题
    hdu 6588 题解
    题解 cf gym 102979 E Expected Distance
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14182274.html
Copyright © 2011-2022 走看看