Django文件上传和下载错误

时间:2019-05-08 04:58:55

标签: django python-3.x

我们使用Django建立了一个前端网站来提供文件处理,但是如果    有很多人(超过2个人)上载文件或下载    文件,将导致系统错误。错误的原因很多,    通常没有这样的文件,...等。请求和重新绑定存在问题。是    有什么解决办法吗?

def download(request):
from django.http import FileResponse
file=open('kmeans/static/upload/example/example.zip','rb')
response =FileResponse(file)
response['Content-Type']='application/octet-stream'
response['Content-Disposition']='attachment;filename="example.zip"'
return response

def zip():
import zipfile
os.chdir(path)
with zipfile.ZipFile(current_time + '-kmeans.zip', 'w') as zf:
    zf.write('Cluster_result.xlsx')
    #zf.write('LDA_visual.html')
os.chdir(os_path)
     return True

 def handle_uploaded_file(file, filename):
    if not os.path.exists('kmeans/static/upload/'+ current_time + '/'):
    os.mkdir('kmeans/static/upload/'+ current_time + '/')
    global path
    path = 'kmeans/static/upload/'+ current_time + '/'

    with open(path + filename, 'wb+') as destination:
        for chunk in file.chunks():
            destination.write(chunk)

enter image description here

0 个答案:

没有答案