Article From:https://segmentfault.com/q/1010000011109862
Question:

Individuals use the ORM model of Django to create a database of relevant business data, and then use its concise method to generate reports. Then visit the Django related pages, get the user’s POST request, read the corresponding data from the database, and then use xlwt orSome graphic modules generate a similar report file locally.
Then how to automatically download the generated report files after Django is implemented and submitted?

Answer 0:

http://blog.csdn.net/qq_18863…
You can refer to this, which is the code that generates Excel files and transmits them. Just associate the POST action with this code, and then you can realize that after posting, the browser automatically pops up the download prompt.

Answer 1:
  1. https://segmentfault.com/q/10…
Answer 2:

postAfter going to the background, read the database data first, then generate the report file as you say, and then spell the path of the file. Next look at the following code, is a section of the download, before the file stream to the server, after the file stream to write to the hard disk. (filename is your documentName, downpath is the file path you spell, just sauce!!
def file_iterator(file_name, chunk_size=512):

    with open(file_name) as f:
        while True:
            c = f.read(chunk_size)
            if c:
                yield c
            else:
                break

Write to the hard disk

response = StreamingHttpResponse(file_iterator(download_path))
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="{0}"'.format(file_name)
return response

Answer 3:

I would like to say my own implementation, first of all, to get the relevant data to generate the report file, then read the file and return to the client, the code is as follows:

def download_file(file_name, root_dir=settings.TMP_PATH):
    """
    Download the file: param file_name: file name: param root_dir: file path: return:"" ""From rexec importFileWrapperFile_name = file_name.replace (",", os.path.sep).Replace ("/", os.path.sep)FilE_str = os.path.sep.join ([root_dir, file_name])If os.path.isfile (os.path.normpath (file_str)):File_name = os.path.basename (file_str).Encode ("UTF-8")Wrapper = FileWrapper (open (FIL)E_str, "RB"))Response = HttpResponse (wrapper, content_type='application/octet-stream')Response['Content-Length'] = os.path.getsize (file_str)Disposition ='attachment; filenaMe=%s'% file_nameResponse['Content-Disposition'] = dispositionReturn responseElse:Raise Http404Class TestViewSet (View):Def post (self, request,*args, **kwargs):'' ''Processing the POST method submitted by the user'' ''Obtaining relevant data in DBAccording to DBGenerate report files by taking related dataIt is assumed here that the generated report file is named'repo.xlsx', generates the path of the report file under the TMP directory in the project, and configuring TMP_PATH = inside the settings.pyOs.path.join (BASE_DIR,'tmp')File_name = u'repo.xlsx'RSP = download_file(file_name)Return RSP

Answer 4:

A variety of ways
1, If you use a Ajax request, you can return the static file name A.xlsx, put your report deposit into /static/../, and the front page is directly window.open (‘/static/../a.xlsx), and you can do it.download

2, Return to the file flow:

from django.http import StreamingHttpResponse

def big_file_download(request):
    def file_iterator(file_name, chunk_size=512):
        with open(file_name) as f:
            while True:
                c = f.read(chunk_size)
                if c:
                    yield c
                else:
                    break

    the_file_name = "file_name.txt"
    response = StreamingHttpResponse(file_iterator(the_file_name))
    return response

Leave a Reply

Your email address will not be published. Required fields are marked *