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

  • 相关阅读:
    css 设置特定宽度,超出部分用...代替
    php 二维数组根据某个key去重
    一些大厂开源项目
    JavaScript如何解析本地xml文件
    console的知识点
    toLocalDateString的用途
    在Vue中使用Object.freeze
    淘宝npm镜像
    JavaScript Async/Await
    vue中的addEventListener和removeEventListener
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14182274.html
Copyright © 2011-2022 走看看