如何在django中进行管理员操作以下载用户的pdf文件 [英] How to make an admin action in django to download user's pdf files

查看:48
本文介绍了如何在django中进行管理员操作以下载用户的pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个管理员操作来下载用户pdf文件用户文件将被上载到媒体目录中什么和管理员应该能够下载任何文件我尝试使用pdfkit让他下载文件,但我无法

I want to create an admin action to download users pdf files The user file would be uploaded in the media directory What and the admin should be able to download any file I tried using pdfkit to let him download the files but I couldn't

> import pdfkit
> 
> def downloadCV(self, request, queryset):
>     projectUrl =  str(queryset[0].cv)+''
>     pdf = pdfkit.from_url(projectUrl, False)
>     response = HttpResponse(pdf,content_type='application/pdf')
>     response['Content-Disposition'] = 'attachment; filename="user_cv.pdf"'

所以我的问题是什么是让管理员下载pdf文件的最佳方法

So my question is what is the best way to let the admin dowload pdf files

我这样尝试

    def downloadCV(self, request, queryset): 
    for x in queryset: 
        projectUrl =  str(x.cv)+''  
    if projectUrl:          
    with open(projectUrl, 'r') as pdf:  
    response = HttpResponse(pdf,content_type='application/pdf')
    response['ContentDisposition']='attachment;filename="user_cv.pdf"'          
    return response 
    pdf.closed

但是我一次只能下载一个文件,是否可以一次下载多个pdf?

but I can only download one file at atime is there is away to download multiples pdfs at once ?

推荐答案

一个请求只能给出一个响应.所以我认为您有2个选择

A request can only give one response. So i think you have 2 options

选项1,您可以发出多个请求.基本上类似于您现在拥有的代码,但是目标是一个文件,但是带有某种javascript代码,这些代码将在新标签页/窗口中的单个文件上运行操作.因此,假设您在管理员中检查了3个文件并运行该操作,则需要打开3个标签,每个标签都有自己的文件,可为您提供pdf.

Option 1, You could make multiple requests. Basically similar to the code you have now but targeting one file but with some kind of javascript code that will run the action on an individual file in a new tab/window. So say you checked off 3 files in the admin and ran the action it would need to open 3 tabs each one its own file serving you the pdf.

选项2,压缩文件并返回一个zip文件.对我来说,这似乎更容易.这是一个我尚未测试过的示例,但您明白了..从queryset中收集文件,然后将其推入zipfile中,然后提供该zipfile.

Option 2, Zip up the files and return that one zip file instead. This seems easier to me. Here is an example that I haven't tested but you get the idea.. gather the files together from the queryset then shove them in a zipfile and then serve up the zipfile.

import pdfkit
import tempfile
import zipfile


def downloadCV(self, request, queryset):
    with tempfile.SpooledTemporaryFile() as tmp:
        with zipfile.ZipFile(tmp, 'w', zipfile.ZIP_DEFLATED) as archive:
            for index, item in enumerate(queryset):
                projectUrl = str(item.cv) + ''
                fileNameInZip = '%s.zip' % index
                pdf = pdfkit.from_url(projectUrl, False)
                archive.writestr(fileNameInZip, pdf)
            tmp.seek(0)
            response = HttpResponse(tmp.read(), mimetype='application/x-zip-compressed')
            response['Content-Disposition'] = 'attachment; filename="pdfs.zip"'
            return response

这篇关于如何在django中进行管理员操作以下载用户的pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆