File Sharing Site in Python

前端 未结 2 1044
梦毁少年i
梦毁少年i 2021-01-14 00:54

I wanted to design a simple site where one person can upload a file, and pass off the random webaddress to someone, who can then download it.

At this point, I have a

2条回答
  •  一整个雨季
    2021-01-14 01:35

    You should send the right HTTP Response, containing the binary data and making the browser react on it.

    Try this (I haven't) if you're using Django:

    response = HttpResponse()
    response['X-Sendfile'] = os.path.join(settings.MEDIA_ROOT, file.file.path)
    content_type, encoding = mimetypes.guess_type(file.file.read())            
    if not content_type:
        content_type = 'application/octet-stream'            
    response['Content-Type'] = content_type            
    response['Content-Length'] = file.file.size            
    response['Content-Disposition'] = 'attachment; filename="%s"' % file.file.name
    return response
    

    Source: http://www.chicagodjango.com/blog/permission-based-file-serving/

提交回复
热议问题